当前位置: 首页 > article >正文

【Python】基于base64对文本进行编码

将字符串转换为Base64编码

import base64
"""
首先将原始字符串转换为字节,然后使用base64.b64encode()方法将字节转换为Base64编码
"""
# 原始字符串
original_string = "Hello, World!"

# 将字符串转换为字节
byte_string = original_string.encode('utf-8')
print("byte_string: ", type(byte_string), byte_string) 
# <class 'bytes'> b'Hello, World!'


# 将字节转换为Base64编码
base64_encoded = base64.b64encode(byte_string).decode('utf-8')
print("Base64编码后的字符串:", type(base64_encoded), base64_encoded)
# Base64编码后的字符串: <class 'str'> SGVsbG8sIFdvcmxkIQ==

将Base64编码的字符串解码为原始字符串

"""
将Base64编码的字符串解码为原始字符串,可以使用base64.b64decode()方法
"""

import base64

# Base64编码后的字符串
base64_string = "SGVsbG8sIFdvcmxkIQ=="

# 将Base64编码的字符串转换为字节
byte_string = base64.b64decode(base64_string)
print("byte_string:" , type(byte_string), byte_string) 
#  <class 'bytes'> b'Hello, World!'

# 将字节转换为字符串
original_string = byte_string.decode('utf-8')
print("解码后的原始字符串:", type(original_string), original_string)
# <class 'str'> Hello, World!


http://www.kler.cn/a/158014.html

相关文章:

  • Linux中的网络配置
  • 帮企多城市分站系统源码+关键词排名优化推广 附带完整的搭建教程
  • LightDB - 支持 last_day 函数[mysql兼容]
  • 【开源视频联动物联网平台】如何解决物联网协议多样性问题
  • LeetCode | 110. 平衡二叉树
  • LiveGBS流媒体平台GB/T28181功能-概览中负载信息直播、回放、播放、录像、H265、级联查看负载会话列表
  • C++作业5
  • Hive_Spark_left()函数
  • 数字营销竞争激烈,这是真的吗?
  • Java中的信号量(Semaphore)机制详解
  • PyQt6 QGroupBox分组框控件
  • 查看php进程占用内存
  • 阿里云新版公共实例从注册账号到创建设备生成参数教程
  • 第73讲:深入理解MySQL数据库InnoDB存储引擎:内存结构、磁盘结构与后台线程全面解析
  • 浪潮信息KeyarchOS——保卫数字未来的安全防御利器
  • Web漏洞分析-SQL注入XXE注入(中上)
  • 【每日易题】Leetcode上Hard难度的动态规划题目——地下城游戏的实现
  • 纹理烘焙:原理及实现
  • 2023-11-28-直播单细胞图表美化-seurat数据结构 featureplot dotplot vlnplot
  • #名词区别篇:事件流事件委托addEventListener白屏时间首屏时间