简体   繁体   中英

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.

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

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.

>>> 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(code)
[231, 131]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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