Python3 implements base64 encoding

The parameter of the b64encode function is byte type, and the characters in python3 are all unicode encoding, so it must be transcoded first. The codes generated by Base64 are all ascii characters.

import base64
s ='nihao'
bs = (base64.b64encode(s.encode('utf-8'))) # Convert characters from unicode encoding to utf-8 encoding

code = (base64.b64encode(s.encode('utf-8'))).decode('utf-8')    #base64编码

print(bs)     -》 b'bmloYW8='

print(code)   -》 bmloYW8=     

That is, the way to achieve base64 encoding is

import base64
s = '字符串'
code = (base64.b64encode(s.encode('utf-8'))).decode('utf-8')  

 

 

Guess you like

Origin blog.csdn.net/qq_44159028/article/details/114669192