Rohan
Rohan

Reputation: 881

python struct.unpack in Java

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?

Upvotes: 0

Views: 1401

Answers (1)

Hans Then
Hans Then

Reputation: 11322

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.

Upvotes: 1

Related Questions