Converting single characters of a string into integers

This is a discussion on Converting single characters of a string into integers within the C Programming forums, part of the General Programming Boards category; Hello, This is my first post in this forum. I have a string s in the following format: -1234567890 Now ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Converting single characters of a string into integers

    Hello,

    This is my first post in this forum.

    I have a string s in the following format: -1234567890

    Now I need to strip the '-' and convert every single digit into an int and place it into an array of ints:

    t[0] = 1
    t[1] = 2
    t[2] = 3
    t[3] = 4
    ...

    For the conversion I tried this inside a for loop, but this didn't work: t[i] = atoi(s[i]);

    Patrick

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Post your code, we'll help you through it.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    atoi(s[i]) will work only if the return value is captured in an int not an array of ints.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Maybe...
    Code:
    t[i] = s[i+1] - '0';
    Assuming s[0] is the '-' char and you loop i from 0 to two less than whatever the length of the string is (including the '-').
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Quote Originally Posted by Dino View Post
    Post your code, we'll help you through it.
    Here is my code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
       int signe;
       int size;
       int *tab;
    } bignum;
    
    bignum tobignum( char a[] ) {
       bignum x;
    
       x.signe = (a[0] == '-');
       x.size = strlen(a);
       if (a[0] == '-') x.size--;
    
       x.tab = malloc(x.size*sizeof(int));
    
       /* removing the '-' if present */
       /* ???? */
    
       /* converting the digits into integers and insering into tab */
       int i;
       for (i=0; i<strlen(a); i++) {
          x.tab[i] = atoi(a[i]);
       }
    
       return (bignum);
    }
    
    int main (int argc, const char * argv[]) {
       char a[50] = "-1234567890";
       bignum n;
       n = tobignum(a);
    
       return 0;
    }
    Patrick

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    hk_mp5kpdw has shown exactly what you need and you don't need so much code for it.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    t[i] = s[i+1] - '0';
    Assuming s[0] is the '-' char and you loop i from 0 to two less than whatever the length of the string is (including the '-').
    But what does the -'0' mean? Is this used to make the conversion?
    And just to get things right, if the string is entered with a scanf command, you also have to remove the \n character at the end, right?

    Patrick

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    But what does the -'0' mean? Is this used to make the conversion?
    If you have the char '0' and wish to get the integer 0, you can subtract '0' from it. Since the digits are guaranteed to be consecutive, '1' - '0' gives 1, and so forth.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by petz_e View Post
    But what does the -'0' mean? Is this used to make the conversion?
    And just to get things right, if the string is entered with a scanf command, you also have to remove the \n character at the end, right?

    Patrick
    If c is of type char and a digit then c - '0' gives you c in its int form otherwise its stored in its decimal ASCII value.
    if c = 1 its decimal ASCII value is 49 but c - '0' gives 1.
    and scanf skips '\n' as its whitespace.
    Last edited by itCbitC; 11-12-2008 at 11:34 AM.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    OK, the input string doesn't have to be composed of consecutive digits, but this should not matter.

    Thanks for the replies and for the help.

    Patrick

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    When I say "the digits are guaranteed to be consecutive", I mean that this guarantee is made of the character set, not your string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Here is a solution...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void convert(char * data, int ** output)
    {
    	int i = 0;
    	for (i = 0; data[i]; i++) {
    		if (data[i] == '-') data++;
    		(*output)[i] = (int)(data[i] & ~0x30);
    	}
    	
    	return;
    }
    
    int main (int argc, const char * argv[]) {
    	char a[50] = "-1234567890";
    	
    	int * output_data = malloc(sizeof(int) * 40);
    	if (!output_data) return 0;
    	
    	convert(a, &output_data);
    	printf("&#37;d\n", output_data[1]);
    	
    	free (output_data);
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by MeTh0Dz View Post
    Code:
    	int * output_data = malloc(sizeof(int) * 40);
    that allocates 4x more storage than needed

  14. #14
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Okay cool. Change it. Although it's not like it really matters. And it also allows for expansion.

    That wasn't even worth commenting on.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    No, it's ok. I got it working now Thanks again

    Patrick

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Converting String to integers
    By Mister C in forum C Programming
    Replies: 21
    Last Post: 06-01-2005, 03:13 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21