• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

int array to byte array

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic