Results 1 to 6 of 6

Thread: ASCII Characters

  1. #1
    Guest

    Post

    How do i display the ASCII code for a character?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try:
    Code:
    char *pcMyString = "Hello World";
    char *p = pcMyString;
    
    while(*(p++)) {
        cout << (int)(*p) << endl;
    }
    If you cast a char to an int, it works fine. Note that it displays a 0 as the last character.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    For some reason when i use (int) it returns

    -6

    every time.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's funny - it works on mine .
    Mine outputs:
    Code:
    101
    108
    108
    111
    32
    87
    111
    114
    108
    100
    0
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    What happened to your 'H', parksie?
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    <twitches>

    Unnkkgghhh...

    Sorry - I wasn't thinking straight:
    Code:
    char *pcMyString = "Hello World";
    char *p = pcMyString;
    
    while(*p) {
    	cout << *p << " : " << (int)(*p) << endl;
    	p++;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width