Problem converting from char array to int array.

This is a discussion on Problem converting from char array to int array. within the C Programming forums, part of the General Programming Boards category; I'm using the following code to do so: Code: #include <stdio.h> int main () { int i; char ch[20]="1 2 ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Problem converting from char array to int array.

    I'm using the following code to do so:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    int i;
    char ch[20]="1 2 34 4 56672332432";
    int ch1[20];
    for (i=0; i<20; i++)
    {
          ch1[i]=(int)ch[i]-'0';
    }
    for (i=0; i<20; i++)
    {
          printf ("%d\n ", ch1[i]);
    }
    return 0;
    }
    The problem is I can't an integer more than 9 as it is. For example, the array is reading and converting each char separately, so for example if I want to get 34 form char array into an integer 34, it won't do so. It picks and gives 3 separately and 4 separately.

    What should I do for this problem?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Use sscanf instead.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Ok, instead of the following code:

    Code:
    for (i=0; i<20; i++)
    {
          ch1[i]=(int)ch[i]-'0';
    }
    I tried the following:

    Code:
    for (i=0; i<20; i++)
    {
    sscanf("%s", ch1);
    }
    and also:

    Code:
    for (i=0; i<20; i++)
    {
    ch1[i] = sscanf("%s", ch);
    }
    But neither works. What would be the proper code?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Let's clarify what you are trying to do. Are you trying to convert the string "1 2 34 4 56672332432" into a sequence of 5 integers, 1, 2, 34, 4 and 56672332432?
    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

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    String would be: ""1 2 34 4 56"

    I want to read this as an int array of size 5.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Ah. Okay, one approach is to use a stringstream. The idea is to treat the string as if it were the contents of a file. For example:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        using namespace std;
        string str = "1 2 34 4 56";
    
        stringstream ss(str);
        int numbers[5];
        for (int i = 0; i < 5; ++i)
        {
            ss >> numbers[i];
        }
    
        for (int i = 0; i < 5; ++i)
        {
            cout << numbers[i] << '\n';
        }
    }
    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

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    This is C++. I'm doing in C.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Right. This thread has been moved to the C programming forum.
    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
    Aug 2009
    Posts
    36
    oh sorry, I didn't know I was in C++ forum! :s

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by TheUmer View Post
    Ok, instead of the following code:

    Code:
    for (i=0; i<20; i++)
    {
          ch1[i]=(int)ch[i]-'0';
    }
    I tried the following:

    Code:
    for (i=0; i<20; i++)
    {
    sscanf("%s", ch1);
    }
    and also:

    Code:
    for (i=0; i<20; i++)
    {
    ch1[i] = sscanf("%s", ch);
    }
    But neither works. What would be the proper code?
    Have you used any scan functions before? Have you even bothered to look up sscanf anywhere at all?

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    #include <stdio.h>
    
    int main ()
    {
        int count = 0;
        int i;
        char ch[20]="1 2 34 4 56";
        int num[20];
        int poz;
        const char* str = ch;
        while(sscanf(str,"%d%n",&num[count], &poz) == 1)
        {
            count++;
            str += poz;
            if(count == 20) break;
        }
        for (i=0; i<count; i++)
        {
            printf ("%d\n", num[i]);
        }
        return 0;
    }
    Last edited by vart; 03-26-2010 at 11:42 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    @vart:

    Thanks a lot. That really worked. I'm highly obliged. Now I can carry on with my assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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