0

Input:

0000001101110010000000100000011110000000100000111000000111110010

Output should be:

302720700838182f

In perl I can do it like this:

unpack("h*", pack("B*", "0000001101110010000000100000011110000000100000111000000111110010"));

8
  • No, this is high nibble first Commented Mar 2, 2022 at 4:53
  • That would give me 0x3720207808381f2 Commented Mar 2, 2022 at 4:54
  • Right, retracted my duplicated flag Commented Mar 2, 2022 at 4:54
  • Your example doesn't make sense to me. Just look at the last 8 bits; how do you go from 11110010 to 82? Guessing at what you mean by "little nibble", 2f would make sense; Commented Mar 2, 2022 at 5:02
  • "302720700838182f" is correct sorry Commented Mar 2, 2022 at 5:08

1 Answer 1

1
"".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) ])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.