Results 1 to 20 of 20
  1. #1
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    67
    Rep Power
    0

    Smile convert String to ASCII value

    Hi,

    Please help me to convert a string value to an ascii value.

    best regards,
    Uthpala

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by uthpalaw View Post
    Hi,

    Please help me to convert a string value to an ascii value.

    best regards,
    Uthpala
    An ASCII value only applies to a single character; please clarify your question.

    kind regards,

    Jos

  3. #3
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    67
    Rep Power
    0

    Default

    ok..
    then i need to convert each character of the string into Ascii values and then concat them to a final value.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by uthpalaw View Post
    ok..
    then i need to convert each character of the string into Ascii values and then concat them to a final value.
    How do you want to concatenate two (or more) ASCII values?

    kind regards,

    Jos

  5. #5
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    67
    Rep Power
    0

    Default

    same order as same as the string appears

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by uthpalaw View Post
    same order as same as the string appears
    Well, the following will do then:

    Java Code:
    StringBuilder sb= new StringBuilder();
    for (int i= 0; i < string.length(); i++)
       sb.append((int)string.charAt(i));
    return sb.toString();
    kind regards,

    Jos

  7. #7
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    67
    Rep Power
    0

    Default

    this also converts the each characters to its ascii values , right ?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Either we are misunderstanding you, or you don't understand what you actually want.
    At the moment I don't know which one it is.

  9. #9
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    67
    Rep Power
    0

    Default

    ok..

    i have a string called s
    String s = "abc";

    and i want each character of this string to be converted into ascii value, if those ascii values are as following,

    'a' = 'asciia'
    'b' = 'asciib'
    'c' = 'asciic'

    so, i want the final string answer as following,

    String finalAsnwer = "asciia"+"asciib"+"asciic";

    i hope this is clear for you.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by uthpalaw View Post
    this also converts the each characters to its ascii values , right ?
    Unless your computer is dangerously wired, why don't you give it a try?

    kind regards,

    Jos

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by uthpalaw View Post
    ok..

    i have a string called s
    String s = "abc";

    and i want each character of this string to be converted into ascii value, if those ascii values are as following,

    'a' = 'asciia'
    'b' = 'asciib'
    'c' = 'asciic'

    so, i want the final string answer as following,

    String finalAsnwer = "asciia"+"asciib"+"asciic";

    i hope this is clear for you.
    That makes no sense at all.

    Are you asking about replaing all 'a' characters (for example) with '97'?
    What on earth use is that?

  12. #12
    uthpalaw is offline Member
    Join Date
    Mar 2010
    Posts
    67
    Rep Power
    0

    Default

    exactly.. is there a way to do that ?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by uthpalaw View Post
    exactly.. is there a way to do that ?
    There you go:

    Java Code:
    String s= "abc";
    System.out.println(s.replaceAll("(.)", "+ascii$1").substring(1));
    kind regards,

    Jos

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default

    Aren't regex clever.
    I can't imagine why the OP would want that.

    @uthpalaw Do you know what an "ASCII" value is? Is it an int, a char, two bytes or ??
    For example what is the "ASCII" value of "A"?
    Have you Googled ASCII to see what that means?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Exactly...why?
    Why do this?
    You'll end up with, for "abc", "979899".
    What use is that?

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default

    "abc", "979899"
    That's base 10. What if he wants it in another base?

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Don't you start...;)

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Don't you start...;)
    What about my great solution in reply #13? Are you all going to ignore it again?

    kind regards,

    Jos ;-)

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default

    Poor JosAH bored and unappreciated again.
    Didn't I say: Aren't regex clever.

    BTW where has the OP gone. Have we chased another one away?

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Poor JosAH bored and unappreciated again.
    Didn't I say: Aren't regex clever.
    Sob, yes, thank you for that; can you say it one more time please?

    Quote Originally Posted by Norm View Post
    BTW where has the OP gone. Have we chased another one away?
    I guess so; it was a lousy question anyway ...

    kind regards,

    Jos

Similar Threads

  1. Convert String to Binary
    By erakhman in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 08:25 AM
  2. convert boolean to string
    By newbiejava in forum New To Java
    Replies: 3
    Last Post: 07-22-2010, 03:32 PM
  3. convert obj to string
    By isme in forum New To Java
    Replies: 11
    Last Post: 06-14-2010, 10:54 AM
  4. Getting ascii from a String
    By ali_sakar in forum New To Java
    Replies: 1
    Last Post: 03-07-2009, 10:11 AM
  5. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-14-2007, 11:26 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
  •