网站首页 > 技术教程 正文
一、Python的标准库
base64模块是用来作base64编码解码,常用于小型数据的传输。编码后的数据是一个字符串,其包括a-z、A-Z、0-9、/、+共64个字符,即可用6个字节表示,写出数值就是0-63.故三个字节编码的话就变成了4个字节,如果数据字节数不是3的倍数,就不能精确地划分6位的块,此时需要在原数据后添加1个或2个零值字节,使其字节数为3的倍数,然后在编码后的字符串后添加1个或2个‘=’,表示零值字节,所以事实上总共由65个字符组成
二、常用方法
- base64.b64encode(str, altchars=None):用于对字节字符串进行Base64编码。
- str:字节字符串
- altchars:替代字符集,用于替换Base64编码中的"+"和"/"字符。
- base64.b64decode(str, altchars=None, validate=False):用于对Base64编码的字节字符串进行解码。
- str:字节字符串
- altchars:替代字符集,用于替换Base64编码中的"+"和"/"字符。
- validate:用于指定是否进行解码前的验证
import base64
org_str = 'hello word'
encode_str = base64.b64encode(org_str.encode())
decode_str = base64.b64decode(encode_str)
print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
- base64.a85encode(str, foldspaces=True, wrapcol=0, pad=False, adobe=False):用于对字节字符串进行Ascii85编码。
- str:字节字符串
- foldspaces:是否折叠空格。
- wrapco:每行的字符数限制。
- pad:是否在编码后添加填充字符。
- adobe:是否使用Adobe的Ascii85编码变体。
- base64.a85decode(str, foldspaces=True, adobe=False, ignorechars=' \t\n\r\x0b'):用于对Ascii85编码的字节字符串进行解码。
- str:字节字符串
- foldspaces:是否折叠空格。
- adobe:是否使用Adobe的Ascii85编码变体。
- ignorechars:要忽略的字符集。
import base64
org_str = 'hello word'
encode_str = base64.a85encode(org_str.encode())
decode_str = base64.a85decode(encode_str)
print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
- base64.b16encode(str):用于对字节字符串进行十六进制编码。
- base64.b16decode(str, casefold=False):用于对十六进制编码的字节字符串进行解码。
- str:字节字符串
- casefold:是否将解码后的结果转换为小写。
import base64
org_str = 'hello word'
encode_str = base64.b16encode(org_str.encode())
decode_str = base64.b16decode(encode_str)
print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
- base64.b32encode(str):用于对字节字符串进行Base32编码。
- base64.b32decode(str, casefold=False, map01=None):用于对Base32编码的字节字符串进行解码。
- str:字节字符串
- casefold:是否将解码后的结果转换为小写。
- map01:自定义的Base32字符映射。
import base64
org_str = 'hello word'
encode_str = base64.b32encode(org_str.encode())
decode_str = base64.b32decode(encode_str)
print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
- base64.b32hexencode(str):用于对字节字符串进行Base32hex编码。
- base64.b32hexdecode(str, casefold=False):用于对Base32hex编码的字节字符串进行解码。
- str:字节字符串
- casefold:是否将解码后的结果转换为小写。
import base64
org_str = 'hello word'
encode_str = base64.b32hexencode(org_str.encode())
decode_str = base64.b32hexdecode(encode_str)
print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
- base64.b85decode(str):用于对Base85编码的字节字符串进行解码。
- base64.b85encode(str, pad=False):用于对字节字符串进行Base85编码。
- str:字节字符串
- pad:是否在编码后添加填充字符。
import base64
org_str = 'hello word'
encode_str = base64.b85encode(org_str.encode())
decode_str = base64.b85encode(encode_str)
print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
- base64.encodebytes(str):用于对Base64编码的字节字符串进行编码。
- base64.decodebytes(str):用于对Base64编码的字节字符串进行解码。
import base64
org_str = 'hello word'
encode_str = base64.encodebytes(org_str.encode())
decode_str = base64.decodebytes(encode_str)
print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
- base64.standard_b64decode(str):用于对标准Base64编码的字节字符串进行解码。
- base64.standard_b64encode(str):用于对字节字符串进行标准Base64编码。
- base64.urlsafe_b64encode(str):用于对字节字符串进行URL安全的Base64编码。
- base64.urlsafe_b64decode(str):用于对URL安全的Base64编码的字节字符串进行解码。
- 上一篇: 使用开源软件和库对AVIF图片进行编码和解码
- 下一篇: Python 中 base64 编码与解码
猜你喜欢
- 2025-01-12 H.265已落后!下一代视频技术实现重大突破
- 2025-01-12 AMD RX 6000显卡也赶上潮流:支持AV1视频编码
- 2025-01-12 Win11 原生支持,微软发布 DirectX 12 全新视频编码 API
- 2025-01-12 视频硬件编码更快但为什么都用软件编码?实测让你搞清楚如何选择
- 2025-01-12 Python实现Base64编码和解码的示例
- 2025-01-12 高通发布aptX Lossless编码,提供CD级蓝牙音质
- 2025-01-12 监控系统中解码器解码上墙操作方法
- 2025-01-12 “姚贝娜病逝”报道的“编码”与“解码”
- 2025-01-12 50个策划人必备的营销模型——编码/解码和用户决策理性/感性
- 2025-01-12 Python 中 base64 编码与解码
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)