int[] to byte[]

Started by
12 comments, last by taybrin 22 years, 2 months ago
In Java, if i have an integer array and a method takes a byte array, is there any way I can convert the integer array to the byte array, short of going through and shifting bits around. I tried casting, but that did not seem to work . . . Thanks
"PC Load Letter, what the F*Ck's that" ~Office Space
Advertisement
i dont imaging there is one... if an integer, 4 bytes, uses each of those bytes, there is no way to convert it to a char (1 byte), without loosing much data (3 bytes worth).

why would you want to do this?
DatagramPackets take byte[] as data. I have data that is in int[] . . . So I figured there would be a function somewhere that coverted the first integer in the int[] to take up the first 4 spaces of the byte array and so on. In C this would not be such a problem i suppose . . .

"PC Load Letter, what the F*Ck's that" ~Office Space
  size_t arraySize = 1024;int* myIntArray = new int[arraySize];char* myCharBuffer = (char*)myIntArray;size_t numberOfBytesInBuffer = arraySize * sizeof(int);  

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
just cast as a pointer to a byte.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
He said in Java. I don''t know how either, I didn''t learn that much Java.

i guess its obvious to me now that there is no easy way . . . thanks for all of your help
"PC Load Letter, what the F*Ck's that" ~Office Space
maybe make a func like this

public byte[] getBytesFromIntArray(int[] data)
{
byte newData = new byte[4 * data.length];

for (int i = 0;i < data.length; i++)
for (int j = 0; j < 4; j++)
newData = (data<i> &amp; <br> (Math.pow(2,(j+1) * 8)) - (Math.pow(2, j * 8)));<br> return newData;<br>}<br><br><br> </i>
No Java guru, but I have an idea.

To get least significant byte, take the modulo (assuming there''s a modulo operator/function in Java) of the integer and 256.

I C, that is
  int var;char b0 = var % 256;  


Then, to get next byte, divide by 256, and take the modulo again. And for the third and fourth byte, divide by 256*256 and 256*256*256 repectively, and take the modulo of it.
  char b1 = (var / 256) % 256;char b2 = (var / 65536) % 256;char b3 = (var / 16777216) % 256;  

There is an easy way, I do this in VB all the time.

Firstly, it makes things a lot simpler if the integer and byte are unsigned. Then:

b3 b2 b1 b0

(bx = byte x)

Mask out b2, b1, b0. Bitshift right 24. This is Byte[0]
Mask out b3, b1, b0. Bitshift right 16. This is Byte[1]
Mask out b3, b2, b0. Bitshift right 8. This is Byte[2]
Mask out b3, b2, b1. This is Byte[3]



Trying is the first step towards failure.
Trying is the first step towards failure.

This topic is closed to new replies.

Advertisement