Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert byte[] to String

Status
Not open for further replies.

SuperMoonster

Programmer
Aug 11, 2005
59
BR
I need to convert a byte[] to String and don't know how.

Using the constructor somewhy doesn't work

Could anyone help? How do you do it? thanks
 
Perhaps using :

String s = new String (byte[[] bytes) ; ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Or perhaps if you define "somewhy does not work"

Cheers,
Dian
 
definition of my "does not work" : prints odd characters in the screen, even when I set the charset.
 
byte[] data by definition may contain non-printable characters - thats why its byte data - not printable ASCII data - thats the point ....

For example, a byte array that is a PNG would not be printable to a screen as readable ASCII data - and neither should it be.

Don't quite get your point - perhaps you should clarify ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Yes, I understand.

But the point is that what's in my byte[] is a simple string. I store it in my database as a BLOB, and my application is manipulating it (or trying to) as a byte[].

So, I store the word "apple" in my byte[], and when I try to turn it into a String using the constructor, I get those odd simbols...

I tried writing a function to convert it, so I wrote this:

Code:
    public String byteString(byte[] meuByte) {
       String str = "";
       int i = 0;
       
       while (i< meuByte.length) {
           str = str + Byte.toString(meuByte[i]);
           i++;
       }
       return str;
    }

It works when I store only numbers... if I store "1974" it does the perfect convertion. But if I store "hello" it returns me blank.

Thanks for any help
 
I can assure you its nothing to do with the byte[] structure, nor the String object - as they say - carp in, carp out - so you must be putting in carp, or incorrectly storing the byte[] in the db, or extracing the data incorrectly.

Also, that function seems a bit odd, I would write it as :

Code:
    public String byteString(byte[] meuByte) {
         return new String(meuByte);
    }

Proof :

Code:
String s = "Hello World";
byte[] b = s.getBytes();
String s2 = new String(b);

System.out.println("in : '" +s +"' , out : '" +s2 +"'");

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
BTW, that function that supposedly converts a byte array to a String is flawed.

Byte.toString() is not doing what you think it is - the method converts the byte to a String representation - which is different to converting it to a string of chars.

So 'H' as a byte is 0x72 - hence when you print it out, it comes as "72" - not 'H'. You need to cast it to a char if you want the 'H'.

In any case, just use the "new String(byte[])" as I said.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top