Hey,
I was hoping someone can help me convert a piece of php code, im not really sure where to start

pack( 'v', $test );

The pack php function above takes in an integer and converts it to an unsigned long.
V - unsigned long (always 32 bit, little endian byte order - is the php documentation of the function. But im told java stores as big endian? So any help on how to convert the integer to an unsigned little endian long would be helpful, i assume a simple type cast would convert to a big endian based number from java.

Integer y = 1;
long x = y.longValue();

The code i am trying but i assume the result is incorrect as java has no signed or unsigned and assumes all values to be signed and the java long is 64 bits not 32? Any guidance would be nice :)

Can you show the input in a java variable and the desired output in hex?
Where is the output of the conversion going?
Are you writing bytes to a file?

Edited 4 Years Ago by NormR1

sorry for the delay, ok the output binary output from:

$binarydata = pack("V", 0x1234);

Results in :
34              12          0           0
00110100    00010010    00000000    00000000



$binarydata = pack("V*", 0x1234,0x344);

Results in :
    44          03          0           0
01000100    00000011    00000000    00000000

More or less what you would expect, only in reverse for some reason i think the functionality problem is going to be cutting the 64 bit long into 32 bits. Since it will lose precision im not sure where exactly to cut? Do i cut off the right?

Since when i use an exceptionally large number the results are the last numbers essentially cutting off the left most bits which are usually the most significant? :

$binarydata = pack("V", 0x12346688992233);

Results:
33              22          99          88
00110011    00100010    10011001    10001000

So any advice on how to basically take a possible large number and pack it into binary string using tobinarystring and cut off only the non significant bits is really the task.

Since Java does not a 32 bit unsigned data type the "right" answer to this depends on what you want to use it for. Is this for writing/reading compatible files?
If the php version is a 32 bit positive integer then a Java long with the same value will have that value in its last 32 bits, and its first 32 bits will be all zero.

e "right" answer to this depends on what you want to use it for. Is this for writing/reading compatible files?
If the php version is a 32 bit positive integer then a Java long with the same value will have that value in its last 32 bits, and its first 32 bits will be all zero.

So if i split the java long in half by converting it to a binary string, removing the left most 32 bits then call that the "packed" binary then it should be equivelant to what would be generated from a positive integer that was packed in php like above? The values the program will be dealing with wont be negative for this data type anyway.

Yes... except that you still need to reverse the order of the 4 bytes that make up the rightmost 32 bits. ie if we lable the 8 bytes of the long as pqrstuvw then the result you need is wvut (assuming, that is, that I understood all this correctly!)

trishtren, I have been trying to figure this out as well, with very little progress so far. I need to convert signed long integers to the 32-bit unsigned little endian binary. They are used for GPS coordinates. Example values are -7838290 and 4042970. Do you have java code that does this? I would love to see it. For the negative number I was told that I should convert the absolute value (so pretty much just multiply by -1), then swap all the 1's for 0's and 0's for ones and add 1 to it. I am not sure how to go about adding 1 to it first off (I am horrible with binary), and the code I found does not output 32 bits either. I am not sure if I should be using some built in java class to convert to binary or what. Please, any help with this that you can give would be greatly appreciated. Thanks.

This question has already been answered. Start a new discussion instead.