Sign in to follow this  
Isolier

[java] Unsigned\Signed Trouble

Recommended Posts

Isolier    125
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.

Share this post


Link to post
Share on other sites
Isolier    125
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.

Share this post


Link to post
Share on other sites
pinacolada    834
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;

Share this post


Link to post
Share on other sites
tombr    148
To convert a (signed) byte to a unsigned byte in a integer, you do this


byte signedByte = -1;
int unsignedByte = signedByte & 0xff;

Share this post


Link to post
Share on other sites
Aldacron    4544
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this