convert int to char in c

Discussion in 'Mac Programming' started by widgetman, Mar 25, 2005.

  1. macrumors member

    Joined:
    Oct 7, 2004
    #1
    Is there a way to convert an integer into a c string? I am not using carbon so none of the cfstr functions will help at all. thanks.
     
  2. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #2
    Do you mean an Interger or an int?

    And do you need to save the string or can you just printf() it...?
     
  3. thread starter macrumors member

    Joined:
    Oct 7, 2004
    #3
    I mean an int. i need to use it as a c string and i wont be printing it out at all. is there a way to do something like cocoa's [NSString stringWithFormat:] and just use %i for the ints?
     
  4. macrumors 65816

    hcuar

    Joined:
    Jul 23, 2004
    Location:
    Dallas
    #4
    Use sprintf... I believe the syntax goes something like this:

    char buffer[100];
    int myInteger = 10;
    sprintf(buffer, "%i", myInteger);

    buffer is a character array, the stuff in the quotes is your string formatting, and myInteger is the integer to format as a string.

    So... if you did the following code:

    cout << buffer << endl;

    You'd see:
    10
     
  5. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #5
    Found this on the net:
    Code:
    char * intToString(int num) 
    
    {
    
    	int i=0;
    
    	int j=0;
    
    	int k=0;
    
    	int ones=0;
    
    	char temp[5]; 					//5=num digits in 32676
    
    	char ans[5];
    
    	
    
    	while (num!=0) 
    
    	{
    
    		ones=num%10;				//get current ones digit
    
    		temp[i]=(char)(ones+48); 	//48=(int)'0';
    
    		num=num/10;					//remove current ones digit
    
    		i++;						//length of number 
    
    	}
    
    	for(j=i-1;j>=0;j--) 
    
    	{
    
    		ans[k]=temp[j]; 			//reorder string correctly
    
    		k++;
    
    	}
    
    	ans[i]='\0'; 					//add null char for end of string
    
    	return (char *)ans;
    
    }
    
    ..but there is a method, I just got to remember what it was called... (it's been a couple of years since I programmed much in c... :eek:)
     
  6. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #6
    Would this work, using the standard C library?
    Code:
    int myint ;
    char mystringbuffer[20] ;
    
    snprintf(mystringbuffer,sizeof(mystringbuffer),"%d",myint) ;
     
  7. macrumors 65816

    hcuar

    Joined:
    Jul 23, 2004
    Location:
    Dallas
    #7
    I still like sprintf the best. Personal preference though.
     
  8. macrumors 65816

    hcuar

    Joined:
    Jul 23, 2004
    Location:
    Dallas
    #8

    NOOOOOOOOOOOOO!!!!!!!!!!!!!! This is way too complicated. :eek:
     
  9. thread starter macrumors member

    Joined:
    Oct 7, 2004
    #9
    Thanks for the quick responses everyone!
     
  10. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #10
    I'm so stupid (forgive me, its 2:30AM, here...) EDIT: And I see this has been pointed out already... by several posters... :D

    You can of course use sprintf()...

    Code:
    int num;
    char myBuf[20];
    sprintf(myBuf, "%i\0", num);
    And when you have it in a char buffer, getting it into a string should be easy, right... ;)

    Edit2: Funny that it 20 digits seems to be enough, right Dr. Q...? ;)
     
  11. macrumors 65816

    hcuar

    Joined:
    Jul 23, 2004
    Location:
    Dallas
    #11
    You don't need the \0 in the format... sprintf automatically adds the null termination...

    BTW... not stupid... just took the long way around. :D
     
  12. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #12
    Yes, I know sprintf() does this for me, but if I've learned one thing from my C/C++ days, it's that it never hurts to terminate the strings manually, too... ;)

    And the real irony, is that i suggested printf() in my first respons, without remembering sprintf()... that's really embarrassing... :eek: ;)
     
  13. thread starter macrumors member

    Joined:
    Oct 7, 2004
    #13
    you are forgiven. thank you again for helping me out with my lame n00b questions. :)
     

Share This Page