python系列之:str、byte相互转换
一、byte转化为str
byte_data = b'c3ff641ecfc1'
str_data = str(byte_data,encoding = "utf-8")
print(str_data)
- 1
- 2
- 3
- 4
输出如下所示:
c3ff641ecfc1
二、str转化为byte
byte_data = bytes(str_data,encoding = "utf-8")
print(byte_data)
- 1
- 2
输出如下所示:
b’c3ff641ecfc1’
三、str、byte相互转换完整代码
byte_data = b'c3ff641ecfc1f9d9e30c761e3bab215d25db0df0242f9285f9e5b2e2876d494036b3b135f599bb7a8e6817d9433385ab'
str_data = str(byte_data,encoding = "utf-8")
print(str_data)
byte_data = bytes(str_data,encoding = "utf-8")
print(byte_data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
输出如下所示:
c3ff641ecfc1
b’c3ff641ecfc1’
四、byte转化hex
byte_data = b'c3ff641ecfc1'
hex_data = byte_data.hex()
print(hex_data)
- 1
- 2
- 3
输出如下所示:
633366663634316563666331
五、hex转化byte
byte_data = bytes.fromhex(hex_data)
print(byte_data)
- 1
- 2
输出如下所示:
b’c3ff641ecfc1’
六、byte、hex相互转换完整代码
byte_data = b'c3ff641ecfc1'
hex_data = byte_data.hex()
print(hex_data)
byte_data = bytes.fromhex(hex_data)
print(byte_data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
输出如下所示:
633366663634316563666331
b’c3ff641ecfc1’
</article>
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/281599.html