Thread: Converting an int to char?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    Question Converting an int to char?

    Hey guys, I need help converting an int to char??

    I have tried using

    Code:
    char dig = (char)(((int)'0')+result);
    which works well, however, it can only convert values from 0-9 if i'm correct.

    I have also tried using the itoa function, but if i use it im gonna need to change a lot of things in my program.

    I cant also use sprintf because I'm not gonna use it for printing alone, I want to push the value to a stack.

    Any suggestions?? Thanks.
    Last edited by mugiwara528; 08-28-2011 at 09:02 AM.

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by mugiwara528 View Post
    Hey guys, I need help converting an int to char??

    I have tried using

    Code:
    char dig = (char)(((int)'0')+result);
    which works well, however, it can only convert values from 0-9 if i'm correct.

    I have also tried using the itoa function, but if i use it im gonna need to change a lot of things in my program.

    Any suggestions?? Thanks.
    sprintf probably could do the trick
    Quote Originally Posted by mugiwara528 View Post
    I cant also use sprintf because I'm not gonna use it for printing alone, I want to push the value to a stack.
    EDIT: oops!
    Last edited by cph; 08-28-2011 at 09:04 AM.
    Do not stare at my avatar.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I use this a lot. Beware the return value is static for convenience.

    Code:
    char *my_itoa (int n) {
    	static char rv[64];
    	sprintf(rv, "%d", n);
    	return rv;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Actually i need to push the converted value to a stack i created, so is it possible to do that using sprintf? I dont want it to print anything

  5. #5
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by mugiwara528 View Post
    Hey guys, I need help converting an int to char??

    I have tried using

    Code:
    char dig = (char)(((int)'0')+result);
    which works well, however, it can only convert values from 0-9 if i'm correct.

    I have also tried using the itoa function, but if i use it im gonna need to change a lot of things in my program.

    I cant also use sprintf because I'm not gonna use it for printing alone, I want to push the value to a stack.

    Any suggestions?? Thanks.
    well you could use a temporary array of chars to store the intermediate result, do some string dump, and push the final result to the stack.
    Do not stare at my avatar.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I you are talking about ascii characters here, not just an 8 bit integer then you can add '0' to get the ascii equivalent.

    Code:
        int a = 9;
        char c = a + '0';
        putchar(c);
    And a char can only hold one ascii character so 0-9 is the possible range of values.

  7. #7
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by mugiwara528 View Post
    Actually i need to push the converted value to a stack i created, so is it possible to do that using sprintf? I dont want it to print anything
    sprintf won't print anything, it stores the result in a buffer
    Do not stare at my avatar.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by mugiwara528 View Post
    Actually i need to push the converted value to a stack i created, so is it possible to do that using sprintf? I dont want it to print anything
    Push a copy of the string returned by MK27's function.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    It's like this

    Code:
    struct node
    {
        char x;
        struct node* next;
    };
    What i did was convert char to int using atoi, which was successful.
    Then, i did some operations with the values and lets say the value after the operation is 15. I want to push that '15' back as a char, is that possible?

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Quote Originally Posted by cph View Post
    sprintf won't print anything, it stores the result in a buffer
    Oh really? Thanks, i'll try that.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Quote Originally Posted by MK27 View Post
    I use this a lot. Beware the return value is static for convenience.

    Code:
    char *my_itoa (int n) {
    	static char rv[64];
    	sprintf(rv, "%d", n);
    	return rv;
    }
    Forgive me but I cant quite understand the function you gave me XD What does static mean? What does the * before the function name mean too?

  12. #12
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by mugiwara528 View Post
    Oh really? Thanks, i'll try that.
    u r welcome
    Do not stare at my avatar.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by mugiwara528 View Post
    It's like this

    Code:
    struct node
    {
        char x;
        struct node* next;
    };
    What i did was convert char to int using atoi, which was successful.
    Then, i did some operations with the values and lets say the value after the operation is 15. I want to push that '15' back as a char, is that possible?
    It depends on if you need to store it as ascii. If not 15 is ok as the range is 256 different values, 127 being the largest positive for a signed char. Your x variable is not a pointer, so the option to store a string location there is out.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Oh! I just realized that now. So i have make my char x; to char x[5]; ?? Man i hope this doesnt mess up my program -_-

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    x[5] should be ok, if you are confident that you will never store anything larger than 9999 (saving the last char for the terminating 0). The benefit of having this space reserved in the node directly is that you get a bit easier memory management since you don't have to allocate memory for the strings dynamically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting char to char*, strcpy problems
    By Gareth321 in forum C Programming
    Replies: 9
    Last Post: 05-24-2011, 12:23 AM
  2. Converting char to int
    By mvanrijnbach in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2010, 04:41 AM
  3. Converting unsigned char[] to char[]
    By Maragato in forum C Programming
    Replies: 8
    Last Post: 09-04-2006, 08:24 PM
  4. Converting from type char to const char*
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2003, 09:51 PM
  5. Converting const char to char array
    By HomerJ in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 03:21 PM