How to encode text to base64 in python

前端 未结 8 2163
悲&欢浪女
悲&欢浪女 2020-12-24 01:17

I am trying to encode a text string to base64.

i tried doing this :

name = \"your name\"
print(\'encoding %s in base64 yields = %s\\n\'%(name,name.en         


        
8条回答
  •  情歌与酒
    2020-12-24 01:38

    It looks it's essential to call decode() function to make use of actual string data even after calling base64.b64decode over base64 encoded string. Because never forget it always return bytes literals.

    import base64
    conv_bytes = bytes('your string', 'utf-8')
    print(conv_bytes)                                 # b'your string'
    encoded_str = base64.b64encode(conv_bytes)
    print(encoded_str)                                # b'eW91ciBzdHJpbmc='
    print(base64.b64decode(encoded_str))              # b'your string'
    print(base64.b64decode(encoded_str).decode())     # your string
    

提交回复
热议问题