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