Click to See Complete Forum and Search --> : a simple question answer required


MeMasterMind
April 20th, 2005, 03:22 AM
Hi all
My question is ..
I want to take the values upto 255 in the byte variable

byte a=127; // works fine
a=255; // here error occurs as possible loss of precision


can some buddy tell me how i can store the values upto 255 or u can say that i want to store unsigned values in the byte variable

wildfrog
April 20th, 2005, 04:03 AM
Java does not support unsigned bytes, but you could do something like this :


//
int i = 255;

// convert to byte
byte b = (byte) i;

// before using, convert byte back to a "unsigned" int
int j = (b < 0) ? 256+b : b;

- petter