简体   繁体   English

将字节数组转换为 int python 错误结果

[英]Convert bytes array to int python wrong result

I know this should be easy, but I just can't get the syntax right for python.我知道这应该很容易,但我就是无法为 python 找到正确的语法。

My int is not converted correctly.我的 int 没有正确转换。 This is the output of my 2 print statements.这是我的 2 个打印语句的 output。 My output should be 9718 instead of 959918392 .我的 output 应该是9718而不是959918392

bytearray(b'9718')
959918392

This is my conversion.这是我的转变。 I don't understand what am I doing wrong.我不明白我在做什么错。

print(size)
print(int.from_bytes(size, byteorder='big'))

What you tried assumes the number is directly encoded as bytes.您尝试的假设数字直接编码为字节。 You actually want to parse it from ascii, which you can do like this:您实际上想从 ascii 中解析它,您可以这样做:

int(b'9718'.decode('ascii'))

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

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