Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Unsigned\Signed Trouble

Started by Isolier Aug 4, 2005 at 5:31 AM 4 replies 550+ views
Original Post
Isolier
Isolier
Hello, In my current project I am reading data from a bitmap via: DataInputStream dataInput = new DataInputStream(new FileInputStream(fileName)); However, I come to notice that some values are being read\interpreted as signed values and I am getting negative numbers where I shouldn't be. For instance, anytime I have a width or height of 128, the number is read as -128 from the bitmap. However, anytime there is another size(I have tested 64 and 256) it is read correctly as a positive number. I know that sounds wierd but that is what is happening and because Java has no specific signed or unsigned data types there is no way to tell Java explicitly how to interpret numbers so as there is no confusion. Anyhow, thank you in advance for your help with this extrememly frustrating problem.
tombr
tombr
Use DataInputStream.readUnsignedByte() or DataInputStream.readUnsignedShort() to get a unsigned value.
Isolier
Isolier
What about reading values larger than short, read two or four shorts for int and long respectively and bit shift? Obviously doable but seems a bit crude. What about byte arrays? I am reading in a byte array and still getting values interpreted as negative values. Now, obviously for a byte array I could instead loop through and read all of the data via DataInputStream.readUnsignedByte() but is there a way to continue to use DataInputStream.read() and get the whole byte array at once? Thanks.
pinacolada
pinacolada
Whether you read the data as signed or unsigned, it's still going to be the same arrangement of bits. It's only when you -use- the number that the sign bit comes into play. Here are some of the operations that are affected by the sign bit (I wish I had a complete list, anyone know of one?):

Up-casting to a larger type (for example, byte to int)
Outputting the number (println)
>, >=, <, <=
Division maybe?
Possibly more?

So if you do any of those things, you'll need special logic to make sure the number is interpreted as unsigned. But for most operations, it doesn't matter. So go ahead and read in that big array of bytes- you can remain blissfully unaware that Java thinks all those bytes should be signed.

Edit: Hmm I realized I'm being a little too vague. If you have a byte that you want to up-cast to an int, you can do something like this:
  int inty = (int) unsigned_bytey;  if (inty < 0) inty += 256;
tombr
tombr
To convert a (signed) byte to a unsigned byte in a integer, you do this

byte signedByte = -1;int unsignedByte = signedByte & 0xff;
Aldacron
Aldacron
Quote:
Original post by Isolier
I know that sounds wierd but that is what is happening and because Java has no specific signed or unsigned data types


For clarity, that's not true. Java types are all signed, except for the char data type which is unsigned.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.