How to Convert array of int into array of byte - please help

I have a 2-dim array of type int and I want to convert it to an array

of byte. How can I do that? Also, if my 2-dim int array is one dimention

can I use typecast as in the example below?

private byte[] getData()

{

byte []buff;

int []data = new int[5];

//populate data[]

buff = (byte[]) data; <- CAN I DO THIS?

return buff;

}

[413 byte] By [Jwael] at [2007-9-26 5:42:52]
# 1
You are somewhat correct in your example except that you can't cast a int array into a byte array. You will have to cast each member of the int array into a byte and place the value in the byte array.Example:byteArray[1] = (byte) intArray[1];I hope this helps;
EsoralTrebor at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 2
Thank you for your response. I have a doubt about the solution.What if the int value is bigger than a byte value, let's say 0xffff, what willhappen? Does it autimatically convert each int into 4 bytes
Jwael at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 3
No, you will get a negative number instead of the real value. If you are converting an int array to a byte array you should be making sure that int value can be safely converted into a byte value. Example:if you cast 159 into a byte you will get -97.
EsoralTrebor at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 4

You are right, thank you. mM int numbers are dynamic and I do not have a control on their value. I need to convert each number into 4 bytes.

I was trying to avoid doing the followong conversion:

buff[i+0] = (byte)(value >> 24);

buf[i+1] = (byte)(value >> 16);

buf[i+2] = (byte)(value >> 8);

buf[i+3] = (byte)(value);

Jwael at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 5

hi, I suggest you make an array of byte arrays

--> Byte[][] and use one row for each number

you can do 2 things,

take each cipher of one number and put one by one in each column of the row correspondent to that number. of course it may take too much room if the int[] is too big, but that is the easiest way I think

the other way is dividing your number into bitsets(class BitSet) with sizes of 8 bits and then you can save each bit into each column of your array. and you still have one number in each row. To put your numbers back use the same class.

Maybe someone has an easier way, I couldnt think of any.

thepatch at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 6
I don't know of any other way either, this may be your best bet.
EsoralTrebor at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 7
Im sorry I mispelled a word on the third paragraph.I meant you can have each byte in each column
thepatch at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 8

What would be handy, is something that was very much possible, when I programmed in Turbo Pascal?

int arr[]=new arr[1000];

byte arr_ref[]=arr;

They would share a memoryspace? Is that possible. It would help for example while using memoryImageSource with int array's I could access the pixel color values, without those <<>&&&&&>&&>&>&> operators.

mikaelkuisma at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 9

No, it's not possible for a byte[] reference and an int[] reference to point to the same object. Java is strongly typed, and in Java arrays are objects. The byte[] class and the int[] class both subclasses of Object, but are otherwise unrelated. You can cast a byte[] reference to an Object reference and vice versa, and similarly for int[], but you cannot cast between int[] and byte[] references.

schapel at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...
# 10
There is a ByteArrayOutputStream in java.io; you can use it with DataOutputStream to write ints... Otherwise doing it yourself with the posted code is the only way.
jsalonen at 2007-7-1 14:00:34 > top of Java-index,Archived Forums,Java Programming...