1 conversion between bytes and hexstr in Python3
- Conversion of Bytetohex
def bytetohex (bins): "" " Convert a byte string to it's hex string representation e.g. for output. " " Return '. Join (["%02x"% x for x in bins]). Strip ()
- Conversion of Hextobyte
def hextobyte (hexstr): "" " Convert a string hex byte values into a byte string. The Hex Byte values may or May is not space separated. "" Return Bytes.fromhex (HEXSTR)
- Test
__hexstr1 = "ffffff5f8121070c0000ffffffff5f8129010b" __hexstr2 = "FF FF FF 5F", 0C XX, FF FF FF FF 5F 81 2 9 0B "__bytestr =" \xff\xff\xff\x5f\x81\x21\x07\x0c\x00\x00\xff\xff\xff\xff\x5f\x81\x29\x01\x0b "if __name__ = =" __ main__ ": print (" \nhex to Byte and byte to Hex Conversion ") print (" Test 1-bytetohex-passed: ", Bytetohex (__b YTESTR) = = __hexstr2) print ("Test 2-hextobyte-passed:", Hextobyte (__hexstr1) = = __bytestr) print ("Test 3 -hextobyte-passed: ", Hextobyte (__hexstr2) = = __bytestr) # turn a non-space separated hex string into a space se parated Hex string! Print ("Test 4-combined -Passed:", Bytetohex (Hextobyte (__hexstr1)) = = __HEXSTR2)
Conversion between bytes and hexstr in Python3