converting byte array to a String

Discussion in 'Java' started by lrantisi, Mar 17, 2007.

  1. lrantisi

    lrantisi Guest

    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.
     
    lrantisi, Mar 17, 2007
    #1
    1. Advertising

  2. lrantisi

    Karl Uppiano Guest

    "lrantisi" <> wrote in message
    news:...
    > 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.
    >


    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[],%20java.lang.String)
     
    Karl Uppiano, Mar 17, 2007
    #2
    1. Advertising

  3. lrantisi

    Karl Uppiano Guest

    "Karl Uppiano" <> wrote in message
    news:Tq_Kh.1526$O_5.1311@trnddc03...
    >
    > "lrantisi" <> wrote in message
    > news:...
    >> 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.
    >>

    >
    > 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[],%20java.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.
     
    Karl Uppiano, Mar 17, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,453
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    678
    Kirby
    Oct 8, 2004
  3. cryptogirl
    Replies:
    5
    Views:
    24,951
    cryptogirl
    Feb 25, 2006
  4. Raga
    Replies:
    3
    Views:
    782
    Thomas Fritsch
    Oct 5, 2006
  5. Replies:
    4
    Views:
    203
    Anthony Jones
    Feb 14, 2006
Loading...

Share This Page