Thursday, May 22, 2014

Exception in thread "SocketThread" java.lang.IllegalArgumentException: 0 > -257
































I get the following error:
































Exception in thread "SocketThread" java.lang.IllegalArgumentException: 0 > -257































at java.util.Arrays.copyOfRange(Arrays.java:3513)































at com.guarddoggps.main.SocketThread.run(SocketThread .java:39)
































I have an array of unsigned chars in c sent over udp socket to java. It looks something like the following (although I changed the array contents a little here):
















































Java Code:





































unsigned char msg[] = {0x28, 0xC2, 0x43, 0x80, 0x0, 0x0, 0xD};



































Now in Java I want to extract this portion of the array:















0x28, 0xC2, 0x43, 0x80, 0x0, 0x0
































So I come up with this:
















































Java Code:





































byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

byte[] resp = packet.getData();
int delimiter = Arrays.binarySearch(resp, (byte)0xD);

byte[] data = Arrays.copyOfRange(resp, 0, delimiter);



































But it thinks the delimiter is out of range from resp byte array. I have no idea where this -257 comes from.














































No comments:

Post a Comment