byte array

I want to insert 0xFF(Hex Value) in a byte array.Such as ,byte[] b=new byte[2];b[1]=0xFF;but when i print out itSystem.out.println(b[1]);Result is: -1but i want it will print out 255.how can i do that?
[264 byte] By [Iftia] at [2007-11-27 7:07:23]
# 1
The maximum number for a byte primitive is 127. The hex number FF is equals to 255. Therefore the value overflows and wraps around to -1.
floundera at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 2
I know it.Please tell me how can i make it possible.Is it possible to insert 255 in a byte arrayand then retrive it
Iftia at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 3
?Since you have tried it and failed and I just told you that the maximum value that a byte can hold is 127, the surely you should be able to ascertain that the answer is NO!Trying using an arry of shorts instead.
floundera at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 4

Thank you very much for response.

I want to tell u the problem in detail.

I have a bitmap field that is constructed by 8 byte.

each byte will hold bitmap for 8 field. Such as i want ot insert 10101010 in the byte. For this reason i want to insert FF in a byte but it seems to be impossible. Please tell me how can i do that. I want to insert 11111111 in a byte and also want to get the content of the byte in the hex form.

Please help me.

Iftia at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 5
I am telling u the detail.How can i insert 11111111 in a byte and also how can i find the value 11111111 from the same byte?Help me.
Iftia at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 6

The byte is an 8-bit, SIGNED, integer. Hence, the first bit is used to denote the sign. Hence, out of the 8 bits, you can only use the 7 bits to represent the value. Hence, 11111111 cannot be held in a byte. Hence, use short instead of byte. This should work, since short is a 16 bit, SIGNED integer and can hold from -32,768 to 32,767. Hope this helps.

java_techya at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 7
System.out.println(b[1] & 0xFF);
jsalonena at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 8
so the Java byte is like a C signed char and there is no Java equivalent for the C unsigned char.
tom_jansena at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...
# 9

Thanks all of u very much

-->>

I have completed my task;

here is the sample code;

byte[] fieldByte=new byte[8];

int[] iField = {0xFF,0xF2}; //Java compiler allows this

for (int i = 0; i < iField.length; i++)

fieldByte=(byte) iField;

receive(fieldByte);

/************************************************/

public static void receive(byte[] holder)

{

System.out.println("\n");

int[] field=new int[holder.length];

for(int i=0;i<holder.length;i++)

{field=unsignedByteToInt(holder);

printIntAsBits(field);

}

}

/*********************************************************/

public static int unsignedByteToInt(byte b) {

return (int) b & 0xFF;

}

/***********************************************************/

public static void printIntAsBits(int value)

{

int mask = 1 ><< 7;

for(int i = 0; i <8; i++)

{

System.out.print((value & mask) == 0 ? "0" : "1");

value <<= 1;

}

System.out.print(" ");

}

/****************************************/

Thanks all of u very much

Iftia at 2007-7-12 18:58:46 > top of Java-index,Java Essentials,New To Java...