Archive for October 2007

Java, Can You Be Any Lamer?

I’m working on making Bunnies an on-line multiplayer game now. In the process I was implementing my messaging system which has one byte for the message type and two bytes for the data length. So data length is equal to byte[1] * 256 + byte[2]. This works on the server side which is written in PHP. This doesn’t work in Java which doesn’t understand unsigned values. For example one message has a length of 400 bytes. So byte[1] = 1 and byte[2] = 144 on the PHP side. When it comes across and Java reads it, it claims that byte[1] = 1 and byte[2] = 253. I have looked at the binary representation of those two numbers and cannot figure out any logical mathematical way to get from 144 to 253 just by manipulating bits.

To verify I’m not going crazy, I changed the length values to be mod 127 instead of 256. Lo and behold it works.

***SOLUTION***

Java uses unicode as the default character set. So when you read in bytes they are converted to unicode values. To fix this you simply use

in = new BufferedReader(new InputStreamReader(conn.getInputStream(), “ISO-8859-1″));