convert byte[] to actual string value

I

IveCal

Hello... How are we going to convert byte[] to it's actual string
value?

ex.

String source = "china";
byte[] bytes = source.getBytes();

// CONVERT the bytes ARRAY to it's actual string value "china"

Please do reply... Thank you very much . . .
 
J

Jan Peter Stotz

IveCal said:
Hello... How are we going to convert byte[] to it's actual string
value?
String source = "china";
byte[] bytes = source.getBytes();

// CONVERT the bytes ARRAY to it's actual string value "china"

String source2 = new String(bytes);

Jan
 
M

Mike Schilling

Jan Peter Stotz said:
IveCal said:
Hello... How are we going to convert byte[] to it's actual string
value?
String source = "china";
byte[] bytes = source.getBytes();

// CONVERT the bytes ARRAY to it's actual string value "china"

String source2 = new String(bytes);

The conversions

String -> byte[]

and

byte[] -> String

both require an encoding, that is a mapping between a char and one or more
bytes. Common encodings are UTF-16, UTF-8, ASCII, ISO-8851-1, etc. The
methods used above, String.getBytes() and new String(byte[]), use the
default encoding for your Java installation, which might or might not be
what you want, particularly because it might not include all characters. As
a rule I've made for myself, I never use the default encoding. Unless the
situation calls for a specific encoding, say the one specified in the header
of an XML document, I always use UTF-8, because it combines representing
ASCII characters efficiently with being able to represent all characters.
 
?

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

Jan said:
IveCal schrieb:

Hello... How are we going to convert byte[] to it's actual string
value?

String source = "china";
byte[] bytes = source.getBytes();

// CONVERT the bytes ARRAY to it's actual string value "china"


String source2 = new String(bytes);

I don't know what does the "actual" mean here. Keep in mind that
the strings are UNICODE.

DG
 
C

Chris Smith

IveCal said:
Hello... How are we going to convert byte[] to it's actual string
value?

ex.

String source = "china";
byte[] bytes = source.getBytes();

// CONVERT the bytes ARRAY to it's actual string value "china"

The first thing you must realize is that there IS no actual string value
for a byte array. There are any number of possible strings that could
be meant by that byte array. Which you get depends on the character
encoding, which is essentially a set of rules for converting characters
to bytes and vice versa.

When you call getBytes() on a String object, the platform default
character encoding is used. The platform default encoding is not
defined by any specification, though it will generally be chosen
consistently withing a single operating system and locale combination.

You can try top recover a String according to the platform default
encoding with:

String s = new String(bytes);

That will work IF the operating system AND the locale are the same as
they were when you converted the String to bytes in the first place.
However, if someone tries to convert that byte[] to a String on a
different operating system, or with a different locale (which can be as
simple as setting the LANG environment variable in any UNIX-like system)
they may well get something completely unintelligible.

The safer way to do it is to pick an encoding and stick with it:

String source = "china";
byte[] bytes = source.getBytes("UTF-8");
String s = new String(bytes, "UTF-8");

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

Dale King

Chris said:
You can try top recover a String according to the platform default
encoding with:

String s = new String(bytes);

That will work IF the operating system AND the locale are the same as
they were when you converted the String to bytes in the first place.

I know you know this Chris, but it is important to point out that for it
to work the platform default encoding must support all the characters
in the string which is not necessarily true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top