• Post Reply Bookmark Topic Watch Topic
  • New Topic

int array to byte array  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to convert an int array to a byte array?

Can I just cast it?

Maybe I should try System.arrayCopy or a ByteArrayOutputStream?

Thanks,

Drew
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A straightforward "for" loop that assigns the elements one at a time, with a cast, is the only sensible way to do it.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't cast the array reference even though you can cast the array elements. I'm a fan of using System.arrayCopy to accomplish this task.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
I'm a fan of using System.arrayCopy to accomplish this task.


'Twould be nice, but it doesn't actually work:



gives



The Javadoc says that you can only copy across different-type arrays if the assignment across the two types can be made by "assignment conversion" -- i.e., without a cast. So you could do it in the other direction (byte[] to int[]) but not int[] to byte[].
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
A straightforward "for" loop that assigns the elements one at a time, with a cast, is the only sensible way to do it.


Won't this result in a loss of information since an int is larger than a byte?

Drew
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Drew Lane:

Won't this result in a loss of information since an int is larger than a byte?


Yes, absolutely, it would, but I assumed you knew that. Were you thinking of converting each int to four bytes, maybe? What did you want to do with the result?
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


Yes, absolutely, it would, but I assumed you knew that. Were you thinking of converting each int to four bytes, maybe? What did you want to do with the result?


Yes, four bytes would be good.

Then I'm thinking of using the resulting byte array in a ByteArrayInputStream.

Drew
[ May 29, 2004: Message edited by: Drew Lane ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to convert each int to four bytes and thus ensure no lost info, you can use a DataOutputStream wrapped around a ByteArrayOutputStream. Loop through each int in the array and use writeInt() - it will be writtenas four bytes, high byte first. When done, use toByteArray() to get the complete array. If / when you want to read the values back into ints later, just use a DataInputStream, which has a readInt() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!