美文网首页
python 字节bytes和字符串string的转换

python 字节bytes和字符串string的转换

作者: Amy_LuLu__ | 来源:发表于2019-08-08 17:21 被阅读0次

原文:https://www.cnblogs.com/skiler/p/6687337.html

import hashlib

#字节对象b
b = b"example"
#字符串对象s
s = "example"
print(b)
print("example")

字符串->字节

b2 = bytes(s,encoding='utf8')  #必须制定编码格式
# print(b2)

b3 = str.encode(s)
print(b3)
print(type(b3))

b4 = s.encode()
print(b4)

字节->字符串

#将字节对象decode将获得一个str对象
s2 = bytes.decode(b)
print(s2)

s3 = b.decode()
print(s3)

相关文章

  • python 字节bytes和字符串string的转换

    原文:https://www.cnblogs.com/skiler/p/6687337.html 字符串->字节 ...

  • Robot framework 内置库String常用方法

    Encode String To Bytes,将字符串(Unicode)编码为字节码(Bytes)。 注意:err...

  • python2和python3区别

    bytes和hex字符串之间的相互转换 在Python2.7.x上,hex字符串和bytes之间的转换是这样的: ...

  • bytes(18)

    在Python3以后,字符串和bytes类型彻底分开了。字符串是以字符为单位进行处理的,bytes类型是以字节为单...

  • 字符串转换base64

    1 先把字符串转换成byte数组 String phone ="0123abc"; byte[] bytes = ...

  • 字符串的构造方法

    字符串的构造方法 1.String(byte[] bytes) 通过字节数组 来创建 byte[] b = {97...

  • 数据存储单位

    获得字符串的字节byte数组 遍历输出bytes时,输出的都是字节byte当string为英文字符时,输出的都是A...

  • 5.bytes和bytearray

    目录1.字符串与字节2.bytes3.bytearray 1.字符串与字节 1.1 字符串与bytes 字符串是字...

  • Python 数据结构 字节

    bytes 不可变字节序列 bytearray 字节数组 可变 字符串与bytes 字符串是字符组成的有序序列,字...

  • Python bytes类型及用法

    Python bytes 类型用来表示一个字节串。“字节串“不是编程术语,是我自己“捏造”的一个词,用来和字符串相...

网友评论

      本文标题:python 字节bytes和字符串string的转换

      本文链接:https://www.haomeiwen.com/subject/mrffjctx.html