aspose file tools
The moose likes Java in General and the fly likes Byte[] to String and Back Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Byte[] to String and Back" Watch "Byte[] to String and Back" New topic
Author

Byte[] to String and Back

sivaprasad pasupulathi
Ranch Hand

Joined: Apr 21, 2009
Posts: 36
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
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4422
    
    2

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

Joined: Apr 21, 2009
Posts: 36
So,what could be my option here of converting to byte[] back.


Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4422
    
    2

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

Joined: Apr 21, 2009
Posts: 36
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

Joined: Aug 11, 2007
Posts: 4422
    
    2

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

Joined: Apr 21, 2009
Posts: 36

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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte[] to String and Back
 
Similar Threads
covert from byte to string and back to byte
Random access reads and writes
need a regular expression for extracting strings.
Are the code pages in charsets.jar documented somewhere?
'&' missing while displaying Textbox values in JSP/jquery/javascript