Results 1 to 4 of 4
  1. #1
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default Convert a String of ASCII Code to Char?

    I am trying to figure out how to convert a string of ASCII code into char.
    I know that you can use (char) to convert it, but the issue is you cannot really just it for Strings.

    I'am very new to Java and have hardly any understanding of it.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,660
    Rep Power
    5

    Default Re: Convert a String of ASCII Code to Char?

    The String class has methods for returning an array of characters or a specific character at a supplied index.
    If you are talking about the actual ASCII code (i.e. 0101 for an 'A') then I am not certain what you mean by
    as string of them. If you do the following:

    Java Code:
    String str = "\101\102"; // the string "AB"
    Then the compiler will do the conversion for you. Then just use the methods I mentioned above.

    Regards,
    Jim
    Last edited by jim829; 08-25-2014 at 03:38 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default Re: Convert a String of ASCII Code to Char?

    Doesn't work for all of it.
    Example: 83 101 110 100 (Decimal)


    83 101 110 100 = Send
    Last edited by Argyros; 08-25-2014 at 04:12 AM.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,660
    Rep Power
    5

    Default Re: Convert a String of ASCII Code to Char?

    The numbers need to be in octal, not decimal.

    Java Code:
    String a = "\123\145\156\144";
    But very rarely is it necessary to do such things.
    Java Code:
          String str = "Send";
          for (int j = 0; j < str.length(); j++) {
             System.out.println(str.charAt(j));
          }
          // or
          for (char c : str.toCharArray()) {
             System.out.println(c);
          }
    You may want to read the tutorials that are linked in my signature.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 4
    Last Post: 09-26-2013, 11:35 AM
  2. convert 30 char of ascii to binary
    By aelynne in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 09:15 AM
  3. convert String to ASCII value
    By uthpalaw in forum New To Java
    Replies: 19
    Last Post: 09-17-2010, 03:02 PM
  4. Convert Char To String
    By fh84 in forum New To Java
    Replies: 15
    Last Post: 10-28-2009, 09:59 PM
  5. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 PM

Posting Permissions

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