converting binary b'0x31303032\n' to "1002" string - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: converting binary b'0x31303032\n' to "1002" string (/thread-13923.html) |
converting binary b'0x31303032\n' to "1002" string - amygdalas - Nov-07-2018 I've tried so many things for hours with out success. Help would be so greatly appreciated. I need to convert the binary type b'0x31303032\n' to "1002" string, which is the hexidecimal translation of 0x31303032 RE: converting binary b'0x31303032\n' to "1002" string - Larz60+ - Nov-07-2018 You can use binascii: import binascii x = b'0x31303032\n' x1 = binascii.unhexlify(x.strip()[2:]).decode() print(x1)results: I should explain a bit:binascii.unhexlify(hexstr) Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of b2a_hex(). hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise an Error exception is raised. x.strip() removes the line feed the [2:] slices off the 0x the decode() at the end flips from bytes RE: converting binary b'0x31303032\n' to "1002" string - amygdalas - Nov-07-2018 THANK YOU! I was so close yet so far. Excellent work. |