Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

convert byte[] to actual string value

Thread view: 
IveCal - 20 May 2006 09:03 GMT
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 . . .
Jan Peter Stotz - 20 May 2006 09:44 GMT
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);

Jan
Mike Schilling - 20 May 2006 15:43 GMT
> IveCal schrieb:
>
[quoted text clipped - 7 lines]
>
> 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.
Dražen Gemić - 20 May 2006 15:58 GMT
> IveCal schrieb:
>
[quoted text clipped - 7 lines]
>
> String source2 = new String(bytes);

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

DG
Chris Smith - 20 May 2006 18:46 GMT
> Hello... How are we going to convert byte[] to it's actual string
> value?
[quoted text clipped - 5 lines]
>
> // 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");

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Dale King - 22 May 2006 03:18 GMT
> You can try top recover a String according to the platform default
> encoding with:
[quoted text clipped - 3 lines]
> 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.

Signature

 Dale King



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.