• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Byte[] to String and Back

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i am trying to convert my pdf bytes[] to string and getting this particular byte[] back in some other class.

while i try to set my initial bytes[] to a string,i do the following

String abc = pdfbytes.tostring();

But abc contains the reference.When i try to get the bytes back from this reference using

Bytes[] def = new String(abc).getbytes();

The size of the previous btye[] and def bytes[] are equal but the data are different.

Is there some where i am going wrong.I want to convert my Byte[] to string and get back the same Byte[] from the string.

Thanks
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
er, byte[] deal with eight bit things, and Strings deal with strings of Unicode character points, which may be 24 bits long.

Its not reliable to simple convert back and forth between byte[] and Java String objects.
 
sivaprasad pasupulathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So,what could be my option here of converting to byte[] back.


 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why convert back and forth? just leave it as a byte[] array.

Or if you need to change it, convert it to something like ArrayList<Byte>

 
sivaprasad pasupulathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Farrell for your response.

I need to convert byte[] to string,because I have to send my response as String to my webservice call.So,i have to convert my byte[] to string before sending across the web-service .I am converting back to byte[] because i want to make sure I deliver what i am passing to client.(testing)

Thanks
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sivaprasad pasupulathi wrote: I need to convert byte[] to string,because I have to send my response as String to my webservice call.



Are you sure? I don't know of any web protocols that transfer Java String objects.

If you have binary data, you need to make it safe for transport, something like MIME encoding.
 
sivaprasad pasupulathi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

I got a work around for this.Hope this helps

byte[] buf = new byte[]{0x12, 0x23};
String s = new sun.misc.BASE64Encoder().encode(buf);

// Convert base64 string to a byte array
buf = new sun.misc.BASE64Decoder().decodeBuffer(s);


This took me 4 hrs of

Thanks
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I hate to say that solution is only half-good. Never use classes in packages that start with sun, sunw or com.sun - these are internal classes that are undocumented and can change or be removed between any two releases.

There is a similar class in Apache Commons Codec: Base64. Use the static encodeBase64String and decodeBase64 for converting.
 
reply
    Bookmark Topic Watch Topic
  • New Topic