Python 3中的字节到整数的转换

问题描述 投票:0回答:1

我有一个名为offset的变量,它属于<class 'bytes'>

我需要找到b / w偏移量与mapper [hash]的差,这里mapper [hash]属于<class 'numpy.int64'>

我具有的功能如下:

for hash, sid, offset in x:
     yield(sid, int(offset) - mapper[hash])

注意:在原始函数中,偏移量不会类型转换为int。我故意这样做是为了让他们与众不同。

但是这抛出一个错误说

ValueError: invalid literal for int() with base 10: b'\xf9\x01\x00\x00\x00\x00\x00\x00'

不足为奇,尽管在调试时我打印了偏移量而没有进行类型转换并找到了->

的值
b'\xcb\x10\x00\x00\x00\x00\x00\x00'
b'B\x10\x00\x00\x00\x00\x00\x00'
b'T\x1c\x00\x00\x00\x00\x00\x00'

该问题的可能解决方案是什么。

numpy type-conversion python-3.6 literals hashlib
1个回答
0
投票

这些值看起来合理吗?

In [292]: alist = [b'\xcb\x10\x00\x00\x00\x00\x00\x00', 
     ...: b'B\x10\x00\x00\x00\x00\x00\x00', 
     ...: b'T\x1c\x00\x00\x00\x00\x00\x00']                                                            
In [293]: [np.frombuffer(astr,int) for astr in alist]                                                  
Out[293]: [array([4299]), array([4162]), array([7252])]

看起来每个字节串是8个字节,所以int64对象的字节表示可能。

© www.soinside.com 2019 - 2024. All rights reserved.