ASCII工具 / 进制转换

Python代码参考

字节数组与十六进制

# 字节数组
byte = b'\x01\x02\x03\x04\x05\x06\x07\x08'
byte = b'hello world'

# 十六进制
h = 0x01020304

# 字符串 -> 字节数组
s = 'hello world'
hb = s.encode('utf8')

# 字节数组 -> 字符串
hb = b'\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64'
s = hb.decode('utf8')

字符串转换

# 字符串 -> 十六进制字符串
# Python2
>>> s = "hello world"
>>> hs = s.encode('hex')
'68656c6c6f20776f726c64'
# Python3
>>> s = 'hello world'.encode('utf8')
>>> hs = s.hex()
'68656c6c6f20776f726c64'
>>> hs = binascii.hexlify(s)
b'68656c6c6f20776f726c64'

# 字符串 -> 十进制字符串
>>> s = "hello world"
>>> ds = ' '.join(['{:d}'.format(ord(c)) for c in s])
'104 101 108 108 111 32 119 111 114 108 100'

# 字符串 -> 二进制字符串
>>> s = "hello world"
bs = ' '.join('{0:08b}'.format(ord(c), 'b') for c in s)
'01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'

# 十六进制字符串 -> 字符串
# Python2
>>> hs = '68656c6c6f20776f726c64'
>>> s = hs.decode('hex')
'hello world'
# Python3
>>> hs = '68656c6c6f20776f726c64'
>>> s = bytes.fromhex(hs)
b'hello world'

# 十六进制字符串 -> 十进制字符串
# Python2
>>> hs = '68656c6c6f20776f726c64'
>>> ds = ' '.join(['%d' % int(hs[x*2:x*2+2], 16) for x in range(len(hs)/2)])
'104 101 108 108 111 32 119 111 114 108 100'
# Python3
>>> hs = '68656c6c6f20776f726c64'
>>> ds = ' '.join(['%d' % int(hs[x*2:x*2+2], 16) for x in range(math.floor(len(hs)/2))])
'104 101 108 108 111 32 119 111 114 108 100'

>>> hs = '68 65 6c 6c 6f 20 77 6f 72 6c 64'
>>> ds = ' '.join(['%d' % int(x, 16) for x in hs.split(' ')])
'104 101 108 108 111 32 119 111 114 108 100'

# 十六进制字符串 -> 二进制字符串
# Python2
>>> hs = '68656c6c6f20776f726c64'
>>> bs = ' '.join(['{0:08b}'.format(int(hs[x*2:x*2+2], 16)) for x in range(len(hs)/2)])
'01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'
# Python3
>>> hs = '68656c6c6f20776f726c64'
>>> bs = ' '.join(['{0:08b}'.format(int(hs[x*2:x*2+2], 16)) for x in range(math.floor(len(hs)/2))])
'01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'

>>> hs = '68 65 6c 6c 6f 20 77 6f 72 6c 64'
>>> bs = ' '.join(['{0:08b}'.format(int(x, 16)) for x in hs.split(' ')])
'01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'

# 十进制字符串 -> 字符串
>>> ds = '104 101 108 108 111 32 119 111 114 108 100'
>>> s = ''.join([chr(int(x)) for x in ds.split(' ')])
'hello world'

# 十进制字符串 -> 十六进制字符串
>>> ds = '104 101 108 108 111 32 119 111 114 108 100'
>>> hs = ''.join(['%02x' % int(x) for x in ds.split(' ')])
'68656c6c6f20776f726c64'

# 十进制字符串 -> 二进制字符串
>>> ds = '104 101 108 108 111 32 119 111 114 108 100'
>>> hs = ' '.join(['{0:08b}'.format(int(x)) for x in ds.split(' ')])
'01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'

# 二进制字符串 -> 字符串
>>> bs = '01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'
>>> s = ''.join([chr(int(x, 2)) for x in bs.split(' ')])
'hello world'

# 二进制字符串 -> 十六进制字符串
>>> bs = '01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'
>>> hs = ''.join(['%02x' % int(x, 2) for x in bs.split(' ')])
'68656c6c6f20776f726c64'

# Python2
>>> bs = '0110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'
>>> hs = ''.join(['%02x' % int(bs[i*8:i*8+8], 2) for i in range(len(bs)/8)])
'68656c6c6f20776f726c64'
# Python3
>>> bs = '0110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'
>>> hs = ''.join(['%02x' % int(bs[i*8:i*8+8], 2) for i in range(math.floor(len(bs)/8))])
'68656c6c6f20776f726c64'

# 二进制字符串 -> 十进制字符串
>>> bs = '01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100'
>>> ds = ' '.join(['%d' % int(x, 2) for x in bs.split(' ')])
'104 101 108 108 111 32 119 111 114 108 100'

# Python2
>>> bs = '0110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'
>>> hs = ' '.join(['%d' % int(bs[i*8:i*8+8], 2) for i in range(len(bs)/8)])
'104 101 108 108 111 32 119 111 114 108 100'
# Python3
>>> bs = '0110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'
>>> hs = ' '.join(['%d' % int(bs[i*8:i*8+8], 2) for i in range(math.floor(len(bs)/8))])
'104 101 108 108 111 32 119 111 114 108 100'

数值转换

# 字符 -> 十进制
>>> s = 'a'
>>> d = ord(s)
97

# 十进制 -> 字符
>>> d = 97
>>> s = chr(d)
'a'

# 十进制 -> 十六进制
>>> d = 123456
>>> h = hex(d)
'0x1e240'

# 十进制 -> 二进制
>>> d = 123456
>>> b = bin(d)
'0b11110001001000000'

# 十六进制 -> 十进制
>>> h = '1e240'
>>> d = int(h, 16)
123456

>>> h = 0x1e240
>>> print(h)
123456

# 十六进制 -> 二进制
>>> h = 0x1e240
>>> b = bin(h)
'0b11110001001000000'

# 二进制 -> 十进制
>>> b = '11110001001000000'
>>> d = int(b, 2)
123456

>>> b = 0b11110001001000000
>>> print(b)
123456

# 二进制 -> 十六进制
>>> b = 0b11110001001000000
>>> h = hex(b)
'0x1e240'