0

I am pretty new to Python and have been trying to port a python script in Java. For a while I have been stuck at the following code logic, trying to convert it to Java but have been unable to do so (probably because I misunderstood what's actually being done)

data = unpack('>H', file.read(2))
if data == 0xffff
then //do something
else //do something else

now, this is I think is being done in the python script above:- unpacking a string (I believe, reading first 2 bytes of a file) in hexa-decimal format and then checking if it is value is 0)

Is my perception about unpacking correct ; if not, then what's exactly unpacking doing? Is it getting a substring from the file object via this operation:-

1 - read the file into a byte array

2 - get the first 2 elements of the byte array

then doing what?

Can someone please help me write down the logic as mentioned in python above in Java?

6
  • Take a look at the struct module. The above code reads 10 bytes of binary data (as a string) from a file and interprets it as a big-endian unsigned short integer. If that integer is equal to 0xffff or 65335 in decimal notation, does something and otherwise something else. Can't help you with the java part. Commented Mar 8, 2013 at 7:48
  • 2
    Are you sure it isn't unpack('>5H', file.read(10)) to unpack 5 big-endian, unsigned short integers into a tuple? As is you'll get an exception. Commented Mar 8, 2013 at 8:20
  • @eryksun: Sorry I mixed 2 lines of code. You are correct, it was '>5H' for reading 10 bytes and '>H' for 2 bytes. Any idea how this could be done in Java? Java, as far as I know does not have unsigned integers in Java (they are BIG ENDIAN and signed). Commented Mar 8, 2013 at 9:37
  • CPython 2.x doesn't have an unsigned short type. It has a small integer type based on a C long and a big integer type that uses a variable array of C unsigned short (if sys.long_info.bits_per_digit is 15). Commented Mar 8, 2013 at 10:02
  • What CPython unpack basically does for format '>H' is to shift in the bytes with bitwise OR (|) and left bitshift (<<). For example: unsigned long x = *bytes++; x = (x << 8) | *bytes;. Then call PyInt_FromLong(x) to create a Python int object. The '5' modifier just instructs it to do this 5 times. Commented Mar 8, 2013 at 10:51

1 Answer 1

1

In java you need a BigInteger, because Java native long will not hold anything larger than 64 bits.

It can be initialized using an array of bytes, so you should be fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.