如何修改字节数组?

编程入门 行业动态 更新时间:2024-10-26 22:16:55
本文介绍了如何修改字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读有关字节和字节数组的内容.我读到了字节​​数组的可变类型!因此,当我尝试对其进行修改时,我收到一条错误消息,提示 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.

更多推荐

如何修改字节数组?

本文发布于:2023-11-02 15:14:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字节

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!