Zbarcea Christian
Zbarcea Christian

Reputation: 9548

Java help to convert UDP packet to int

I'm receiving an UDP packet (in which format I don't know, I think UTF-16 -little endian-), only thing that I know is the following doc. directly from the developers page:

The master servers each respond by sending FF FF FF FF 73 0A followed by a (relatively) unique 4-byte "challenge" number.

So this is how I'm receiving the packet:

byte[] buff = new byte[64];
DatagramPacket packet = new DatagramPacket(buff, buff.length);
socket.receive(packet);

Packet was received, everything is Okay, but now I'm stuck. I need that 4 byte integer. I must split the buffer or... I don't know what to do.

This is the received data:

˙˙˙˙s
Ň?t

I tried to convert to hex but the output is very interesting:

-0000000000000000000000000008cf52df7c08c

Method to convert:

public String toHex(String arg) throws UnsupportedEncodingException {
   return String.format("%040x", new BigInteger(arg.getBytes("UTF-16LE")));
}

Than I tried to convert hex to string (from the method above) and the result is much more interesting (sorry I can't copy paste, something goes wrong), anyway the method used to convert hex to string is:

public String hexToString(String hex){
   StringBuilder output = new StringBuilder();
   for (int i = 0; i < hex.length(); i+=2) {
      String str = hex.substring(i, i+2);
      output.append((char)Integer.parseInt(str, 16));
   }

   return new String(output);
}

So with all that said, I'm stuck. I don't know what am I supposed to do. Am I need to split the UDP packet in to pieces or...?

Upvotes: 0

Views: 1972

Answers (1)

Stephen C
Stephen C

Reputation: 718946

I'm receiving an UDP packet (in which format I don't know, I think UTF-16 -little endian-), only thing that I know is the following doc.

You really need to find out what the packet actually contains. The packet contents you have posted in your question don't make much sense to me, and don't seem to correspond to the supposed format.

Start out by dumping the bytes of the byte array like this:

byte[] bytes = ...
int len = // number of bytes read.
for (int i = 0; i < len; i++) {
    System.out.format("%02x ", bytes[i]);
}

Then compare that with the expected format from the documentation. If they match (more or less) then you can start on the problem of deciding how to extract the data that you need. Otherwise, you first need to figure out what the format REALLY is. Maybe we can help ... but we need a reliable rendering of the packet (e.g. produced as above.)


FWIW, the reason that you are getting -0000000000000000000000000008cf52df7c08c is (I think) that BigInteger(byte[]) is interpreting the byte array as a signed number. Any way, that's a not a good way to do this. The UDP packet body is a sequence of bytes, not a number.


I also think it is unlikely that the UDP packet is UTF-16. FFFF is described thus in the official Unicode code charts:

Noncharacters:

These codes are intended for process-internal uses, but are not permitted for interchange. [...]

FFFF : • the value FFFF is guaranteed not to be a Unicode character at all

So if someone is claiming that this is UTF-16, the usage is violating the Unicode standard.

Upvotes: 1

Related Questions