1

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 displays as 4294967294

I know FF should be the marker for - but is there a method in python that can just read the values directly or do I have to make my own method?

Thank you!

Edit: This is for Python 2.6. My program reads from binary data and I am just displaying it in hex to make it simpler. The program simply reads 4 bytes at a time and grabs values from those 4 bytes. It is just some of those values are negative and display the above numbers. I am also hoping someone can explain how Python interprets the binary data into a value so I can write a reverse protocol. Thank you!

I read from hex and convert to values through this method.

def readtoint(read):
    keynumber = read[::-1]
    hexoffset=''
    for letter in keynumber:
        temp=hex(ord(letter))[2:]
        if len(temp)==1:
            temp="0"+temp
        hexoffset += temp
    value = int(hexoffset, 16)
    return value

It grabs 4 bytes, inverses the order, then converts the hex value into a int value. THe values I posted above are inverted already.

user1150764
  • 103
  • 1
  • 4
  • 10
  • 6
    Show your code, please! As it is, the question is quite unclear. Does the file contain the strings "FFFFFFFF", or does it contain binary data? How are you extracting the information? – Sven Marnach May 31 '12 at 11:52
  • 4
    How are you reading the values? If you have bytes objects you can use struct.unpack(">i", your_bytes)[0] to get the signed value... – LexyStardust May 31 '12 at 11:53
  • 1
    my guess is you are reading strings from the file since `int('FFFFFFFF', 16)` gives `4294967295` – Levon May 31 '12 at 11:55
  • This is for Python 2.6. My program reads from binary data and I am just displaying it in hex to make it simpler. The program simply reads 4 bytes at a time and grabs values from those 4 bytes. It is just some of those values are negative and display the above numbers. I am also hoping someone can explain how Python interprets the binary data into a value so I can write a reverse protocol. Thank you! – user1150764 May 31 '12 at 12:03
  • @user1150764 Thanks for clarifying this .. you may want to move your comment and make it part of your original post, not everyone will read/find comments. – Levon May 31 '12 at 12:07
  • 1
    But that still doesn't explain how you 'grab values' from your bytes. – Thomas K May 31 '12 at 12:08
  • I added the method in how it converts to values. I hope that helps :). – user1150764 May 31 '12 at 12:12
  • Thanks, that's clearer. As others have pointed out, you want to use the `struct` module to deal with this sort of thing: http://docs.python.org/library/struct.html – Thomas K May 31 '12 at 12:23

2 Answers2

6

Use the struct module:

import struct

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

Example:

>>> readtoint('\xfe\xff\xff\xff')
-2
Martin Vilcans
  • 5,428
  • 5
  • 22
  • 17
0

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

Community
  • 1
  • 1
Maria Zverina
  • 10,863
  • 3
  • 44
  • 61