I am creating a UDP packet to configure a GPS modem in java. One of the sections of the packet is a phone number and I have been given the follow information:
The Phone number is 64 bits.
Byte 7 is = 0x00 (big endian).
Bytes 4-6 = area code (little endian).
Bytes 0-3 = other digits (little endian).
What I have done so far:
The Phone number is : 123 4567890 I converted the byte sections into hex: 123 = 0x00007B 4567890 = 0x0045B352
ByteBuffer UDPConfigModem = ByteBuffer.allocate(8);
byte areaCode1 = (0x00007B >> 8) & 0xFF;
byte areaCode2 = (0x00007B >> 16) & 0xFF;
UDPConfigModem.putInt((byte) 0x0045B352).order(ByteOrder.LITTLE_ENDIAN);
UDPConfigModem.put((byte) areaCode2).order(ByteOrder.LITTLE_ENDIAN);
UDPConfigModem.put((byte) areaCode1).order(ByteOrder.LITTLE_ENDIAN);
UDPConfigModem.put((byte) 0x00007B).order(ByteOrder.LITTLE_ENDIAN);
UDPConfigModem.put((byte) 0x00);
Because the other digits requires 4 bytes I just used a putInt and ordered the bytes, I am pretty sure that is not the problem.
The area code requires 3 bytes and there isn't a variable that I know of that has that, so I read up on bit shifting and used the logic from How do I convert a 24-bit integer into a 3-byte array? to shift it. This is where I believe my problem is.
Byte 7 is pretty straight forward.
I should be getting a response back from the modem if the phone number is sent correctly, but when monitoring it on wire shark I see the packet send but no response is sent back.
I was wondering if you guys could see any errors or better ways to go about this?
Thanks.
Your code is confusing. A byte isn't little or big endian. Endianness only applies to a sequence of bytes representing a larger number such as an Integer. .order
on ByteBuffer
swaps the entire buffer which isn't what you want.
putInt((byte) 0x0045B352)
isn't going to work - you'll probably just get the lower byte of that integer. I suggest you use this answer https://stackoverflow.com/a/4378416/116509 and put the bytes in one-by-one (there should be no reason to use a cast).