Byte Array to String and vice versa – Java

17 09 2008

This is little stupid, but I wasted some time on this. The problem is

String s = "abc123";
byte[] b = s.getBytes();
System.out.println(b.toString());

Output: [B@3e25a5

You don’t get back the original string when you use toString().

If you want to get back the original string, you will have to use string constructor:

String s = "abc123";
byte[] b = s.getBytes();
System.out.println(new String(b));

Output: abc123.

Advertisements

Actions

Information

One response

19 09 2008
Shridharan

dude, i was looking into the same prob couple of days back…thats coool šŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: