Python3 Coding problem Unicode utf 8 bytes interconversion method

  • 2021-01-02 21:55:13
  • OfStack

Why this article is needed, because when docking some very old interfaces, you need to pass the hex string in hexadecimal, and you need to encode the passed string, here is the introduction of ES2en-8 Unicode bytes and so on.


# English use utf-8  Converted to 16 Into the system hex Methods of strings 
newstr = 'asd'
b_str = bytes(newstr,encoding='utf-8')
print(b_str)
hex_str = b_str.hex() # will bytes Type into 16 Into the system hex string 
print(hex_str) # Turn the bytecode 16 Into the system hex The method of 
print(bytes.fromhex(hex_str).decode('utf-8')) # will 16 Into the system hex String conversion to bytes, And then convert it to a string 
print(type(' Chinese '.encode('utf-8')),' Chinese '.encode('unicode_escape'),' Chinese 123456'.encode('unicode_escape').decode('utf-8'))

# Convert Chinese into Unicode the 1 Kind of method of 1
u_str = ' Chinese 123456'
b_str = bytes(u_str,encoding='unicode_escape')
h_u_s = b_str.hex()print ("\u4e2d\u6587") #Unicode The code can be output directly 
# Chinese use Unicode Converted to bytes To convert 16 Into the system hex methods   Contains English and numbers 
u_cn = ' Chinese asd123'
hex_msg = bytes(u_cn,encoding='utf_16_be').hex() 
# This is the final solution under special requirements 
# Note that in Python3 I don't have any strings in there that I can just turn into bytes or Unicode The method of the 
# In other words, in Python In the  u' Chinese ' It doesn't work anymore 

#bytes turn str
b_str = bytes(' Chinese ',encoding='utf-8')
print(b_str.decode()) # The direct output is a normal string 

Related articles: