Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data.
(byte_string, spaced=False)
| 1 | import binascii |
| 2 | |
| 3 | def hex_from_bytes(byte_string, spaced=False): |
| 4 | """Return the hexadecimal representation of the binary data. Every byte of |
| 5 | data is converted into the corresponding 2-digit hex representation. The |
| 6 | resulting string is therefore twice as long as the length of data. |
| 7 | """ |
| 8 | hex_string = binascii.hexlify(byte_string).decode('ascii') |
| 9 | |
| 10 | if spaced: |
| 11 | hex_string = ' '.join([hex_string[i:i+2] for i in range(0, len(hex_string), 2)]) |
| 12 | |
| 13 | return hex_string |
| 14 | |
| 15 | def bytes_from_hex(hex_string, spaced=False): |
| 16 | """Return the binary data represented by the hexadecimal string. Every 2-digit hex representation |