如何修改字节数组? [英] How can i modify a byte array?

查看:86
本文介绍了如何修改字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关字节和字节数组的内容.我读到了字节​​数组的可变类型!因此,当我尝试对其进行修改时,我收到一条错误消息,提示 integer是必需的我在这里缺少什么吗?以下是我的代码和错误

I was reading about bytes and byte array's. I read that byte arrays mutable types! so, when i am trying to modify it i am getting an error saying integer is required Am i missing something here? The following is my code and the error

z=bytearray("hello world","utf-8")
z[0] ="H"

我遇到了以下错误

TypeError跟踪(最近一次调用最后)在()----> 1个z [0] ="H"

TypeError Traceback (most recent call last) in () ----> 1 z[0]="H"

TypeError:必须为整数

TypeError: an integer is required

推荐答案

文档显示:

字节数组类型是 integers 的可变序列,范围为0< = x<256.

The bytearray type is a mutable sequence of integers in the range 0 <= x < 256.

将每个字符转换为ASCII 整数值时可以使用字符串创建的原因.因此,当分配'H'时,实际上是指分配 72 .

The reason you can create it with a string as each character is converted to its ASCII integer value. So when assigning 'H' you actually mean to assign 72.

如果您希望能够分配字符,则只需将每个字符首先传递给 ord().

If you want to be able to assign chars, then just pass each one into ord() first.

这篇关于如何修改字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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