• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting an Array with any datatype to byte array

 
Aravind M
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I have an array with different datatypes and I need to convert this array into a byte array and use it and again revert back to the original. how do I do that. Any suggestions are welcome.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a job for object serialization, just write the array to an ObjectOutputStream to a ByteArrayOutputStream then use the toByteArray() method. All the little details are taken care of automatically and it is surprisingly fast.

Bill
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Aravind",
Please check your private messages.
 
Yelamuri Chandu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check here some sample cofe
ByteArrayOutputStream bs = new ByteArrayOutputStream()
bs.write(.. .. ..);


byte [] bArr = bs .toByteArray();

Again you can create ByteArrayOutputStream with the above byte array.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Aravind M",

Please check your private messages.
 
Abhishek Chattopadhyay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone elaborate on the above given example how does it work ?
thanks in advance. :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic