binascii modules /binascii module
The Binascii module contains a number of conversion methods between binary and ASCII -encoded binary representations . You can refer to some built-in functions for binary conversions.
hexlify with the unhexlify
>>>ImportBinascii>>> A ='Hello World'>>> B =Binascii.b2a_hex (a) Traceback (most recent): File"<pyshell#2>", Line 1,inch<module>b=Binascii.b2a_hex (a) TypeError:'Str'Does notSupport the buffer interface>>> A = b'Hello World'>>> B =Binascii.b2a_hex (a)>>>Print(b) B'68656C6C6F20776F726C64'>>> B =binascii.hexlify (a)>>>Print(b) B'68656C6C6F20776F726C64'>>>Print(binascii.unhexlify (b)) b'Hello World'
The above functions hexlify and B2a_hex are actually a function, it is recommended to use Hexlify. Its role is to return the hexadecimal representation of the binary data . The data for each byte is converted to the corresponding 2-bit hexadecimal representation. So the resulting string is twice times the length of the original data. A2b_hex and Unhexlify perform the reverse operation.
Related reading
1. Built-in functions
Python's functional module [3]--BINASCII conversion