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 / March 2007

Tip: Looking for answers? Try searching our database.

converting byte array to a String

Thread view: 
lrantisi - 17 Mar 2007 23:38 GMT
What's the best and fastest way to convert a byte array to a String.
The (toString doesn't work, it gives strange characters).

I prefere a way that uses a loop that loops in the bytes of the array
and convert them to characters then append them to a string.

Thanks.
Karl Uppiano - 18 Mar 2007 00:01 GMT
> What's the best and fastest way to convert a byte array to a String.
> The (toString doesn't work, it gives strange characters).
[quoted text clipped - 3 lines]
>
> Thanks.

How about one of the variants of this, which allows you to specify a
character set encoding?

http://java.sun.com/javase/6/docs/api/java/lang/String.html#String(byte[],%20jav
a.lang.String
)
Karl Uppiano - 18 Mar 2007 00:06 GMT
>> What's the best and fastest way to convert a byte array to a String.
>> The (toString doesn't work, it gives strange characters).
[quoted text clipped - 8 lines]
>
> http://java.sun.com/javase/6/docs/api/java/lang/String.html#String(byte[],%20jav
a.lang.String
)

That URL was supposed to take you to this String constructor:

public String(byte[] bytes, String charsetName)
      throws UnsupportedEncodingException

The charsetName tells the constructor how to interpret the bytes in the byte
array -- they could be UTF-8, ASCII, EBCDIC, or Swahili, which explains the
strange characters you get from toString, which just uses the default
encoding for your platform.


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.