字符串与ASCII码转换
#ascii转字符串
a_ascii = 97
b = chr(a_ascii)
print(b)
=>a
#字符串转ascii
abc = 'a'
x = ord(abc)
print(x)
=>97
对于一个长的字符串使用如下:
import numpy as np
str = 'hello world'
ascii = np.fromstring(str, dtype=np.uint8)
print(ascii)
=>[104 101 108 108 111 32 119 111 114 108 100]
字符串转换为十六进制
a = 102
print(hex(a))
=>0x66
#带有 float() 函数的 hex() 函数将 float 值转换为十六进制
a = 102.18
print(float.hex(a))
=>0x1.98b851eb851ecp+6
#十六进制的字符串并想将其转换为十六进制的数字
hex_s = '0xEFA'
a = int(hex_s,16)
hex_n = hex(a)
print(hex_n)
=>0xefa
s= 'Sample String'.encode('utf-8')
print(s.hex())
=>53616d706c6520537472696e67