Skip to content Skip to sidebar Skip to footer

Reading Negative Values From A File In Python

I am trying to read some negative values from a compressed file that has the hex values: FFFFFFFF, which should be -1, but displays as 4294967295 FFFFFFFE, which should be -2, but

Solution 1:

Use the struct module:

importstruct

def readtoint(read):
    returnstruct.unpack('<i', read)[0]

Example:

>>>readtoint('\xfe\xff\xff\xff')
-2

Solution 2:

Post you file reading code to get the perfect answer. But answer to your question is almost certainly here:

Reading integers from binary file in Python

Post a Comment for "Reading Negative Values From A File In Python"