It's not a secret anymore!
The moose likes Beginning Java and the fly likes Byte array to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte array to String" Watch "Byte array to String" New topic

Byte array to String

Jonathan Gu

Joined: Apr 01, 2002
Posts: 6
I have a simple question.
I wonder how Java transfer from byte array to String?
please give a example
Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
Well, suposse you have an array with these byte values: 65,66,67,68,69,70,71
If you call Stringļæ½s constructor: String(byte[] bytes), when you pass your array mentioned above you will obtain a string value constructed by adding characters corresponding to each unicode value in your array.
65 = A, 66 = B, 67 = C, ...

Sigfred Zamo<br />Software Development<p><a href="" target="_blank" rel="nofollow">My page</a>
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Jonathan Gu:
I wonder how Java transfer from byte array to String?

One of the *way cool* features of the JDK is the included source code of the API. In my JDK 1.3 installation, the source code files were in a jar file called src.jar. In my JDK 1.4 installation, the source code files were in a zip file called
If you were to unzip either of these files (for information on working with jar files, see
Using JAR Files: The Basics), you'd be able to browse the source code for all of the standard API classes.
Looking at the source code for the class String, and following the trail of method invocations to get to the dirty work of converting the bytes to a String eventually leads to references to platform specific code which is not included in the src.jar or file. A lot of the platform specific code is available, but let's not go there now...
The good news is that a lot of the functionality of the standard API classes is not platform specific, and one can learn a lot about how to solve problems in Java by browsing the source code of the classes in the API.
Good Luck.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Byte array to String
It's not a secret anymore!