简体   繁体   English

将bytes()(Python 3字符串)输出更改回int列表

[英]Changing bytes() (Python 3 string) output back to list of ints

I am building a zipping software, and i am having trouble changing back my bytes to ints. 我正在构建一个压缩软件,但无法将字节变回整数。 i used the function bytes(), on an list of ints, the function returns a byte, but now when i am trying to decode it i am getting an error. 我在整数列表上使用了函数bytes(),该函数返回一个字节,但是现在当我尝试对其进行解码时,我得到了一个错误。

example: 例:

    code = bytes([231, 131])
    code.decode()

and than i am getting the next error: 而且比我得到下一个错误:

unicodeDecoderError: 'utf-8' codec can't decode bytes in postion 0-1: unexpected end of data unicodeDecoderError:'utf-8'编解码器无法解码位置0-1中的字节:数据意外结束

I am not understanding the whole bytes thing to good, so be nice. 我不了解整个字节的事情,所以要好。 thank you 谢谢

In Python 3, one can easily convert from a list of integers to the bytes type. 在Python 3中,可以轻松地将整数列表转换为字节类型。

>>> code = bytes([231, 131])
>>> code
b'\xe7\x83'
>>> type(code)
<class 'bytes'>

Coercing the bytes type to list converts the unicode back to a list of integers: 将字节类型强制转换为list会将unicode转换回整数列表:

>>> list(code)
[231, 131]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM