Python字符串到字节数组并返回 [英] Python string to bytearray and back

查看:119
本文介绍了Python字符串到字节数组并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将人类可读的字符串转换为字节数组并返回?

How can I convert a human readable string to a bytearray and back?

说我有 Hello World,想要一个字节数组,然后从一个字节数组转换为

Say I have "Hello World" and want a bytearray and then from a bytearray to the same string?

推荐答案

您可以使用 bytearray()

You can use bytearray():

b_array = bytearray('yoyo')
for elem in b_array:
    print elem


要将 b_array 转换回字符串格式,请使用 .decode()


To convert b_array back to string format use .decode():

for elem in b_array.decode():
    print elem

这篇关于Python字符串到字节数组并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆