python binascii binary conversion instance
- 2021-06-28 13:06:43
- OfStack
As follows:
#coding:utf-8
import binascii
a = 'worker'
# First put worker convert to 2 Binary data is then used 106 Binary representation
b = binascii.b2a_hex(a)
print b
# and b2a_hex Contrary
print binascii.a2b_hex(b)
# This function and b2a_hex()1 kind
c = binascii.hexlify(a)
print c
# This function and a2b_hex()1 kind
print binascii.unhexlify(c)
###### Run Results ######
>>> 776f726b6572
worker
776f726b6572
worker
python Built-in functions:
hex():
# hold 10 Binary Conversion to Shaping 16 Binary
>>> hex(88)
'0x58'
# Convert Floating Point to 16 Binary
>>> 1.23.hex()
'0x1.3ae147ae147aep+0'
# Built-in function hex and binascii.hexlify() The difference is that,
#hex Strings can only be accepted by integers but not by strings
>>> hex('88')
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
hex('88')
TypeError: hex() argument can't be converted to hexbin():ba
bin(): hold 10 Binary integer conversion 2 Binary character
# hold 10 Binary Integer Conversion 2 Binary
>>> bin(88)
'0b1011000'
>>> bin(33)
'0b100001'
oct(): hold 10 Binary Conversion 8 Binary character
# hold 10 Binary Conversion 8 Binary
>>> oct(500)
'0764'
>>> oct(488)
'0750'
chr(): hold 1 Integer conversion ASCII The corresponding single character in the code table
# hold 1 Integer conversion ASCII The corresponding single character in the code table
>>> chr(98)
'b'
>>> chr(97)
'a'
ord(): and chr Instead, put ASCII Convert characters in code table to corresponding integers
>>> ord('b')
98
>>> ord('c')
99