In Java, char is unsigned -- it is essenitally unsigned short, but I think you really mean that byte in Java is signed, which is true, even though a byte that was unsigned would make mor sense for most uses of byte. The fix is simple: to convert a byte to an int value in the range [0,255] (ie, to treat a byte as unsigned) write:So your code could be written:That being said, check out DataInputSteam -- it may eliminate the need for doing this entirely! [ January 18, 2006: Message edited by: Jeff Albrechtsen ]
There is no emoticon for what I am feeling!
Joined: Jul 01, 2003
Hi Jeff, Thanks for a quick response. Is the return value a decimal value?
Joined: Sep 16, 2005
It's an int. One doesn't speak of an int being a decimal or hex value because that doesn't make any sense. That's referring to the string format of a number. An int is an int.
Originally posted by John McDonald: Hi Jeff, Thanks for a quick response. Is the return value a decimal value?
Technically, an int (or any data for that matter) is stored as binary. You should try compiling this code in a simple test framework and check if the int has the excpected decimal value that you would like for various byte inputs.
I arranged the bytes in that order because I was mimicking your code: Perhaps your question really is: which way are the bytes of a int stored? The answer is that it depends. Java prefers to store hi-order bytes first (big-endian).
Originally posted by Jeff Albrechtsen: It's an int. One doesn't speak of an int being a decimal or hex value because that doesn't make any sense. That's referring to the string format of a number. An int is an int.
You can concievably use either of these. The order of the bytes depends on whether the value is represented in big-endian or little-endian format. If you are interested, you should google for more information on these.