Reputation: 97
Input:
0000001101110010000000100000011110000000100000111000000111110010
Output should be:
302720700838182f
In perl I can do it like this:
unpack("h*", pack("B*", "0000001101110010000000100000011110000000100000111000000111110010"));
Upvotes: 0
Views: 479
Reputation: 240649
"".join([ ("%02x" % int(x,2))[::-1] for x in re.findall(r'.{8}', bits) ])
I'm sure there's a nicer way, but it works: extract 8 bits at a time from the input, use int(..., 2)
to parse them as binary, then format as hex, then swap the nibbles ([::-1]
), then stick it all back together.
alternatively, if you find the use of re
here ugly:
"".join([ ("%02x" % int(bits[i:i+8],2) )[::-1] for i in range(0, len(bits), 8) ])
Upvotes: 1