Thread: Character to Ascii

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    Character to Ascii

    Hello there, I'm stuck trying to convert any scanned character to ascii. I know that printf("%d", a); will print the value of the letter a (that is 74 or 75), but i can't get to save it.

    This is where I'm at
    Code:
    int x;
    char letter;
    print("enter a letter\n");
    scanf("&c", letter);
    x = (int)letter;
    but that doesn't seem to work.

    any input is highly appreciated,

    thanks.!
    Last edited by Dave_Sinkula; 07-10-2006 at 03:06 PM. Reason: Fixed [code][/code] tags.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Change scanf("&c", letter); to

    scanf("&c", &letter);

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    are you sure the ASCII value of a is 74 or 75? this code prints 97.

    Code:
    char letter;
    printf("enter a letter\n");
    scanf("%c", &letter);
    printf("%d", letter);

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    He may be talking hex, but I think that's in and around the 60's ...
    Last edited by twomers; 07-10-2006 at 02:42 PM.

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    I believe a is 0x61 in hex.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    yeah sorry. it's 97. I think 74 was for 'A'. but yeah I did have that code..

    I just can't figure out how to save what's being printed as an integer x or (in the case of scanning a, x should be set to 97).

    thanks for the quick replies too.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The character has an integer value.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char letter = 'a';
       printf("letter = '%c' (using %%c)\n", letter);
       printf("letter = %d (using %%d)\n",  letter);
       printf("letter = %#x (using %%x)\n", letter);
       return 0;
    }
    
    /* my output
    letter = 'a' (using %c)
    letter = 97 (using %d)
    letter = 0x61 (using %x)
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. Replies: 1
    Last Post: 07-31-2002, 10:49 AM