0

Possible Duplicate:
Why does byteArray have a length of 22 instead of 20?

I just cant figure this out Why Am I getting an error when converting like this

String mystring = "This is a String";
Log.v("string:", mystring);
Log.v("byte then string:", mystring.getBytes().toString()); 

The output is as follows

String: This is a string
byte then string: [B@44ecc390

Can anyone help ? I actually just want a string to be converted to bytes and the again to string.

Community
  • 1
  • 1
Saaram
  • 337
  • 3
  • 7
  • 29

1 Answers1

0

you should use

Log.v("byte then string:", new String(mystring.getBytes()));

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • No, you shouldn't - that will use the platform default encoding, which is almost never appropriate. – Jon Skeet Dec 09 '12 at 09:26
  • 1
    Assuming the method used was `byte[] bytes = String.getBytes()`, it consistent to use `new String(bytes)` otherwise, `String.getBytes("UTF-8")` should be used in the first place. – aymeric Dec 09 '12 at 09:33
  • @JonSkeet yes it is true, I was giving an example. You should always specify encoding when converting byte array to string. new String(byteArray, "UTF-8"). – Subin Sebastian Dec 09 '12 at 09:42
  • @aymeric: It's only consistent if the bytes were obtained on a platform which uses the same encoding. I would personally always specify the encoding explicitly, even if it's *explicitly* saying "use the platform default". That states intentions more clearly, IMO. – Jon Skeet Dec 09 '12 at 09:54