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?
0xffffor65335in decimal notation, does something and otherwise something else. Can't help you with the java part.unpack('>5H', file.read(10))to unpack 5 big-endian, unsigned short integers into atuple? As is you'll get an exception.unsigned shorttype. It has a small integer type based on a Clongand a big integer type that uses a variable array of Cunsigned short(ifsys.long_info.bits_per_digitis 15).unpackbasically 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 callPyInt_FromLong(x)to create a Pythonintobject. The '5' modifier just instructs it to do this 5 times.