Thread: cast string to int

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    cast string to int

    i have an array with character numbers.. i tried casting:

    Code:
    printf("%d", (int)string[0]);
    but the above returns 0 and not the result desired.. Any ideas on how to properly cast a character from a string to an int?

    example:

    Code:
    	char crap[12];
    	crap[0] = '1';
    	
    	printf("\n%d\n", (int)crap[0]);
    Last edited by Axel; 09-15-2006 at 08:08 AM.

  2. #2
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    nevermind.. i used atoi but that converts the whole string to a number. how do i convert numbers individually?

  3. #3
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Code:
    printf("\n%d\n", crap[0] - '0');

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hi,
    thanks i'm still curious as to how you would store it in a variable?

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by Axel
    nevermind.. i used atoi but that converts the whole string to a number. how do i convert numbers individually?
    Well, that's easy. Just subtract the ASCII value of zero from that particular character as demonstrated below.
    Code:
    	char crap[12];
    	crap[0] = '1';
    	
    	printf("\n%d\n", crap[0]-'0');

  6. #6
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by Axel
    nevermind.. i used atoi but that converts the whole string to a number. how do i convert numbers individually?
    If you wanted to convert all the contents of your character array, why not place it inside a for loop, then with respect to the index, perform the conversion.
    Code:
    Noob - a word used to describe someone like me. (noun)

  7. #7
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    U mean something like this
    Code:
    #include <stdio.h>
    int main()
    {
       char crap[] = "123";
       int biggerCrap = crap[0] - '0';
       printf("\n%d\n", biggerCrap);
       return 0;
    }

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    yes thanks.. hmm i've never seen - '0' before. what/where exactly does it refer to?

  9. #9
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    watch the Ascii table and U will understand. '0' is char and it's 0x30 in ascii and crap[0] is '1' also char and in ascii represantation is 0x31. Substracting the two U get the right result
    Last edited by andor; 09-15-2006 at 08:47 AM.

  10. #10
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm..i see. can it also be used dynamically? tried the following without any luck
    Code:
    	for (i=0; i< length; i++)
    	{
    		total =+ crap[i] - ''+ i +'';
    	}
    empty character constant?

  11. #11
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    No. But U can do like this
    Code:
    char tmp[2] = {'\0', '\0'};
    for (i=0; i< length; i++)
    {
       tmp[0] = crap[i]; 
       total += atoi(tmp);
    }

  12. #12
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21

    Wink

    Quote Originally Posted by andor
    No. But U can do like this
    Code:
    char tmp[2] = {'\0', '\0'};
    for (i=0; i< length; i++)
    {
       tmp[0] = crap[i]; 
       total += atoi(tmp);
    }
    But make sure to declare those variables first. hihi.
    Code:
    Noob - a word used to describe someone like me. (noun)

  13. #13
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm still doesn't work..
    Code:
    	char tmp[2] = {'\0', '\0'};
    	char crap[2]  = {'1', '3'};
    	int total;
    	int i;
    	for (i=0; i< 2; i++)
    	{
       		tmp[0] = crap[i]; 
       		total += atoi(tmp);
    	}
    	
    	printf("%d", total);
    returns 3. when it should be 4 ? :|

  14. #14
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    U forgott just a little thing
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    	char tmp[2] = {'\0', '\0'};
    	char crap[2]  = {'1', '3'};
    	int total = 0;
    	int i;
    	for (i=0; i< 2; i++)
    	{
       		tmp[0] = crap[i]; 
       		total += atoi(tmp);
    	}
    	
    	printf("%d", total);
    	return 0;
    }
    by must go home C ya

  15. #15
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i never knew not initializing i to 0 would cause so much trouble. thanks ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM