Archived

This topic is now archived and is closed to further replies.

taybrin

int[] to byte[]

Recommended Posts

taybrin    122
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

Share this post


Link to post
Share on other sites
EvilCrap    134
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?

Share this post


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

Share this post


Link to post
Share on other sites
DrPizza    160
  
size_t arraySize = 1024;
int* myIntArray = new int[arraySize];
char* myCharBuffer = (char*)myIntArray;
size_t numberOfBytesInBuffer = arraySize * sizeof(int);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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[i * 4 + j] = (data &
(Math.pow(2,(j+1) * 8)) - (Math.pow(2, j * 8)));
return newData;
}


Share this post


Link to post
Share on other sites
Brother Bob    10344
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;

Share this post


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

Share this post


Link to post
Share on other sites
DrPizza    160
Oh fuck, I didn''t notice it was Java.

Given the lack of pointers and the lack of unions, there is no fast way of doing this, short of shifting.

Sucktacular.

Share this post


Link to post
Share on other sites
EvilCrap    134
oh, lol, you were talking about casting, i have no idea for that in java. i imaging u could do the bit mask thing though.

Edited by - evilcrap on January 30, 2002 3:16:27 PM

Share this post


Link to post
Share on other sites
CaptainJester    523
No there isn''t. I check the API docs for the Integer and Byte classes to see if there were any utility functions for this, but there isn''t It looks like you are stuck with:
  
int i; // integer to be converted

byte b[]=new byte[4]; //result


//this assumes the the msb is first

//if you are writing the receiver as well, then it doesn''t matter

b[0]=(byte)i>>24;
b[1]=(byte)i>>16;
b[2]=(byte)i>>8;
b[3]=(byte)i;


For a better source of Java answers, you can join the Java Developer Connection for free and ask this question on their forums. They are Java specific.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Share this post


Link to post
Share on other sites
taybrin    122
Thanks for everyone's replies. Turns out in Java it seems that all primatives, except for the "char" are signed. Thus I cannot just cast a byte to an int and shift. I tried this unsuccessfully, various ways.

So what I ended up doing is the following, and yes I know it looks ugly but it works. Since chars are unsigned I can throw the byte into a char and the actual bit representation of the byte will hold. Anyways, I think i can get rid of the bitwise &, but this seems to work:

  
public static int[] byteToInt(byte[] bArray) {
int[] retInts = new int[bArray.length / 4];
int index = 0;
for(int i = 0; i < bArray.length; i+=4) {
char lb = (char)(0x00FF & bArray[i]);
char lb1 = (char)(0x00FF & bArray[i+1]);
char lb2 = (char)(0x00FF & bArray[i+2]);
char lb3 = (char)(0x00FF & bArray[i+3]);
char left = (char)(lb << 8 | lb1);
char right = (char)(lb2 << 8 | lb3);
int x = left << 16 | right;
retInts[index++] = x;
}
return retInts;
}


Again, thanks for all of your help, and if you have any suggestions please speak up.



Edited by - taybrin on January 31, 2002 5:30:57 PM

Share this post


Link to post
Share on other sites