SoFunction
Updated on 2024-11-10

Python3 encoding issues Unicode utf-8 bytes conversion method

Why do you need this article, because in the docking of some very old interfaces, you need to pass over the hexadecimal hex string, and the requirements of the string passed to do the encoding, here introduced utf-8 Unicode bytes and so on.

#Converting a hexadecimal hex string from utf-8 to hexadecimal using the English language
newstr = 'asd'
b_str = bytes(newstr,encoding='utf-8')
print(b_str)
hex_str = b_str.hex() # Convert bytes types to hex strings in hexadecimal
print(hex_str) #Bytecode to hexadecimal hex method
print((hex_str).decode('utf-8')) # Convert hexadecimal hex strings to bytes, and then convert them to strings.
print(type('Chinese'.encode('utf-8')),'Chinese'.encode('unicode_escape'),'Chinese 123456'.encode('unicode_escape').decode('utf-8'))

#One of the ways to convert Chinese to Unicode
u_str = 'Chinese 123456'
b_str = bytes(u_str,encoding='unicode_escape')
h_u_s = b_str.hex()print ("\u4e2d\u6587") # Unicode encoding for direct output
#Convert Unicode to bytes and then to hexadecimal hex method for Chinese, including English and numbers.
u_cn = 'chineseasd123'
hex_msg = bytes(u_cn,encoding='utf_16_be').hex() 
# This is the ultimate solution for special requirements
# Note that in Python 3 there is no longer a direct way to turn strings into bytes or Unicode
# That is, u'Chinese' no longer works in Python.

#bytes to str
b_str = bytes('Chinese',encoding='utf-8')
print(b_str.decode()) # Output directly as a normal string

Above this Python3 encoding problem Unicode utf-8 bytes mutual conversion method is all I share with you, I hope to give you a reference, and I hope you support me more.