int[] to byte[]

Started by
12 comments, last by taybrin 22 years, 2 months ago
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.
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 ***/
Advertisement
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
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 convertedbyte b[]=new byte[4];  //result//this assumes the the msb is first//if you are writing the receiver as well, then it doesn''t matterb[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
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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
"PC Load Letter, what the F*Ck's that" ~Office Space

This topic is closed to new replies.

Advertisement