Results 1 to 8 of 8
  1. #1
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default Converting ASCII format

    I don't find a way to convert byte[] array into ASCII format, i.e. String.

    String message=New String (abytearray);

    is it suitable??

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    String message = "";
    byte[] abytearrray = { 1, 4, 6, 9, 1 };
    for(byte element:abytearray) {
    message += Byte.toString(element);
    }
    That should work.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    yes new String(byteArray) is quite suitable.

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Oh that works?
    Ok that would go faster then. Do what Fubarable said.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    for instance:
    Java Code:
      public static void main(String[] args)
      {
        byte[] hexArray =
        {
          0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
          0x73, 0x74, 0x20, 0x74, 0x6f, 0x20, 0x73, 0x65, 0x65, 0x20, 0x69, 0x66,
          0x20, 0x6e, 0x65, 0x77, 0x20, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x28,
          0x62, 0x79, 0x74, 0x65, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x29, 0x20,
          0x77, 0x6f, 0x72, 0x6b, 0x73, 0x2e
        };
        System.out.println(new String(hexArray));
      }

  6. #6
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default Thanks to Forbearable

    Your test code is really creative. Thank you ;)

  7. #7
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default Correction

    Sorry for typo Fubarable. Can I ask you why you use 0x before Ascii codes?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    0x is not used for ASCII codes, it's to tell Java that the number is in hexadecimal format.

Similar Threads

  1. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 11:21 AM
  2. I need help with ascii characters
    By Grandon in forum Eclipse
    Replies: 17
    Last Post: 11-08-2008, 03:12 AM
  3. Ascii code.........
    By Somitesh Chakraborty in forum New To Java
    Replies: 6
    Last Post: 11-04-2008, 06:32 PM
  4. Converting to ASCII and vice-versa
    By pheonix in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 05:43 AM
  5. Getting ASCII codes of character
    By gapper in forum New To Java
    Replies: 1
    Last Post: 02-02-2008, 10:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •