Search code examples
numpytype-conversionpython-3.6literalshashlib

Bytes to Int conversion in Python 3


I have a variable named offset that belongs to <class 'bytes'>.

What I need is to find the difference b/w offset and mapper[hash], here mapper[hash] belongs to <class 'numpy.int64'>.

The function I have is as follow :

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

Note : In the original function offset isn't typecasted to int. I have purposely done that to get difference b/w them.

But this is throwing an error saying

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

Not surprising, though on debugging I printed the offset without typecasting and found the values to ->

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'

What are the possible solutions to this problem.


Solution

  • Do these values look reasonable?

    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])]
    

    Looks like each bytestring is 8 bytes, so may be the byte representation of int64 objects.