Thread: Is there a way to convert a char?

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    Question Is there a way to convert a char?

    Hi, I am with a doubt that i have since yesterday, is there a way to transform a char value to an int value, for example, 2x, i want to get the two, but it is declared as a char, so i do:
    a=2
    b=x

    after that, being "a" also a char value, is there a function that let me change it to an int?

    thanks

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I believe atoi() does the trick.

    but char and int is (relatively) the same thing.

    Try: printf("%d", 'A'); or printf("%c", 65);

    www.asciitable.com shows all the integer values for each character, and if you want, you could make a function that converts a character to your own specified integer based off those values.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm gathering you want something like:
    Code:
    char a = '1';
    int b = 1;
    /* do something */
    /* a == b is now true */
    If thats what you want you can subtract the value of '0' from a
    Code:
    char a = '1';
    int b = 1;
    a -= '0';
    /* now a == b */
    I believe it's been stated that the values of '0' - '9' must be in ascending sequentical values.

  4. #4
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Code:
    #include <stdlib.h>
    
    int main()
    {
    char ch[2];
    int i;
    
    strcpy(ch, "2x");
    
    ch[1] = '\0';
    
    i = atoi(ch);
    
    printf("%c	-	%d", ch[0], i);
    
    return 0;
    }
    One way to do it.... easy to understand.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by dagdarian
    Code:
    #include <stdlib.h>
    
    int main()
    {
    char ch[2];
    int i;
    
    strcpy(ch, "2x");
    
    ch[1] = '\0';
    
    i = atoi(ch);
    
    printf("%c	-	%d", ch[0], i);
    
    return 0;
    }
    1) No need to use strcpy as you could easily initalize the array with the values
    2) should be int main(void) if you aren't using command line arguements
    3) Please don't provide the full solution if the OP hasn't provided any code of their own.
    One way to do it.... easy to understand.

  6. #6
    </life>
    Join Date
    Oct 2004
    Posts
    83
    The code is valid for example purposes,

    strcpy was used on the assumtion that "2x" wasn't a predefined string and to show that the original character string did not have to be altered to use this method. Leaving open the option that ch could simply be a temporary variable (i.e. for use in a function)

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by dagdarian
    The code is valid for example purposes,

    strcpy was used on the assumtion that "2x" wasn't a predefined string and to show that the original character string did not have to be altered to use this method. Leaving open the option that ch could simply be a temporary variable (i.e. for use in a function)
    But it's not valid. The strcpy() copies three chars to a two-char array:

    '2', 'x', 0

    Also, you should #include <string.h> to use strcpy(). (And #include <stdio.h> to use printf().)

    Regards,

    Dave
    Last edited by Dave Evans; 11-03-2004 at 09:41 AM.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    hmm... why not just use sscanf()?
    Code:
    char szStr[] = "123abc";
    int a;
    
    sscanf(szStr, "%d", &a); // Will read 123 into a

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM