Thread: Cast an int to char

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    69

    Cast an int to char

    Is it possible to get the string representation of an int. So I mean if i have:

    5, I want to get '5'.

    I was searching in the internet and I found this solution:

    Code:
    int number = 5;
    char c = number + '0';
    which works fine for small numbers, but if I do:

    Code:
    int i;
    char c;
    for(i =0; i < 10000; i++) {
    c = i + '0';
    printf("%c\n", c);
    }
    At some point some strange sings like %&/)/)§$%&) are appearing, But i really need this huge numbers as well .. Is there any other way to do this??

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Look into the function "sprintf()".

    (Note: Your examples deal with single characters, not strings.)

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming you have something like;
    Code:
       int value = 10000;
       char buffer[some_big_enough_value];     /*  for value of 10000, some_big_enough_value must be 6 or more */
    Try
    Code:
       sprintf(buffer, "%d", value);      /*  sprintf(), like printf() is declared in <stdio.h> */
       printf("%s\n", buffer);
    or
    Code:
       itoa(value, buffer,10);      /*  itoa() is declared in <stdlib.h> */
       printf("%s\n", buffer);
    or even (since itoa() returns its second argument)
    Code:
       printf("%s\n", itoa(value, buffer,10));
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    May 2014
    Posts
    69
    Undefined symbols for architecture x86_64:
    "_itoa", referenced from:
    _main in main.o
    ld: symbol(s) not found for architecture x86_64

    But sprintf does it for me. thank you really much!!!!!!!!

  5. #5
    Registered User
    Join Date
    May 2014
    Posts
    69
    And what if I want to do it the other direction, is an (int) enough?
    Wow this is a really nice method. thank you a lot. I also managed to cast a address to a char:

    Code:
        int lala = 0;
        
        printf("%p \n", &lala);
        
        char buffer[100];
        sprintf(buffer, "%p", &lala);
        printf("%s\n", buffer);
    Can you give me some advice how to do these things but in the other direction? so char -> pointer, char -> int
    Last edited by tinchi; 10-02-2014 at 01:30 PM.

  6. #6
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    I know it's off-topic but could u please look into this thrad: Command line argument parsing issues?

  7. #7
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Quote Originally Posted by tinchi View Post
    And what if I want to do it the other direction, is an (int) enough?

    Nope. atoi() is modulo 2^32. Ex:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
     unsigned int v=atoi("9876543210");
    	printf("%u\n",v);
    	return 0;
    }
    gives 1286608618 which is 9876543210%(1<<32)
    code that worx upto 64 bit (unsigned long long) can be written by you (no library function exists on that size)

    greater than that you'll need arbritary precision numbers. (If you dont know what that is, dont ask and be satisfied with numbers that fit in ints)

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    atoi() is modulo 2^32
    atoi() is undefined if the result does not fit into an int; lots of systems will reduce it modulo 2**32, but that's not a requirement. Also, int need not be 32 bits wide (it could be smaller or larger).

    code that worx upto 64 bit (unsigned long long) can be written by you (no library function exists on that size)
    In C99 (which introduced (unsigned) long long to standard C), there exist the functions strtoll() and strtoull(), which convert a string to long long/unsigned long long. In addition, they have more advantages over atoi(): overflow is not undefined and is detectable; you can specify the base; and you can determine where exactly the parsing stopped, so you can tell if the entire string was converted.

  9. #9
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Quote Originally Posted by cas View Post
    In C99 (which introduced (unsigned) long long to standard C), there exist the functions strtoll() and strtoull(), which convert a string to long long/unsigned long long. In addition, they have more advantages over atoi(): overflow is not undefined and is detectable; you can specify the base; and you can determine where exactly the parsing stopped, so you can tell if the entire string was converted.
    Cool. I'll have to look into that someday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cast from String to char
    By chercheur in forum C Programming
    Replies: 2
    Last Post: 08-04-2012, 04:44 PM
  2. Cast to char from int
    By TeeGee in forum C Programming
    Replies: 6
    Last Post: 02-23-2010, 02:02 AM
  3. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  4. how to cast a char to an int
    By rozner in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 03:50 PM
  5. How do I cast a char Array[] to a const char* ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2001, 08:43 AM