Hi all. Im writing a server for a multiplayer game. Information is sent in packets of 256 bytes. The first 4 bytes of each packet is an integer number representing the ID of this packet. When a packet is send a local copy is stored in a hashmap based on the integer ID. When the server recieves a packet out of sequence it requests a resend of the packet its been expecting which is taken from the hashmap and resent.
The problem is that for certain numbers the id comes out wrong and I cant work out why.
One example is for 144. The id is 144, it gets put into the byte array and stored int the hashmap and send. The local copy in the hashmap and the message sent to the server come out as 63. Does anyone have any idea why it might do this?
I'm going to gout on a limb here and say 144 is beyond the value of a byte?
Recently, in my company we were working on a program that reads a barcode value and decodes it, we run our program on two platforms, one for linux and one for windows and we noticed something strange. The barcode we were reading is referred to using hex values and as such we had one with the hexvalue of 0x81 .. of course 81 is beyond the limits of a byte and will be a negative value in byte which is fine..
The strange thing is how a byte is read by a String class. In Windows the value we entered returned as 63 or a "?" and in Linux it returned as blank. This really only happened after we convert the byte array into a string as we tested the byte array and the values are correct, though negative for values exceeding 127.
In short, negative byte values do not get translated correctly when passed into a String. I think this has to do with the fact that the ascii chart generally reaches to 127 and anything beyond that is platform dependent? Occasionally with negative byte values i get some weird Strings not in the standard ascii char set.
What you can perhaps do is before using it as a hash key is to convert it into a positive short or int value.. thats what we did and might not be the best way to do it
I hope this helps.
p/s : Sorry if i am not clear, i'm hopeless in explaining things... [ September 22, 2006: Message edited by: Darren Cheong ]
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop