Split string into a char array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dschu012
    New Member
    • Jul 2008
    • 39

    Split string into a char array

    Ignore the title it isn't exactly what I meant, and after reading it the title sounds kinda stupid.

    I have an list of numbers as an ascii string that I need to split by a token and store the result in an array of chars. However I want the value of the char to be the number the ascii text represents. i.e. if the text within the tokens is '240' I want the value of the char to be 0xf0. The main problem is I don't know how to split the strings and I don't know how to convert the value to what I want.

    This is an example input

    string s= "0,0,0,0,240,24 0,240,240,0,0,0 ,0,255,255,255, 255,0,0,0,0,80, 80,80,80,0,0,0, 0,80,80,80,80"

    this is the output that I would want

    char data[4][8] =
    {0x0,0x0,0x0,0x 0,0xf0,0xf0,0xf 0,0xf0,
    0x0,0x0,0x0,0x0 ,0xff,0xff,0xff ,0xff,
    0x0,0x0,0x0,0x0 ,0x50,0x50,0x50 ,0x50,
    0x0,0x0,0x0,0x0 ,0x50,0x50,0x50 ,0x50};

    As of now this is how I would approach it

    Code:
    string s= "0,0,0,0,240,240,240,240,0,0,0,0,255,255,255,255,0,0,0,0,80,80,80,80,0,0,0,0,80,80,80,80";
    char data[4][8];
    int i,j;
    char result;
    char * tok;
    
    tok = strtok(s,',');
    for(i=0;i<4;i++)
       for(j=0;j<8;j++)
       {
          if(tok != NULL) {
             //convert tok into it's value as a char somehow and store it in result
             char[i][j] = result;
             tok = strtok(NULL,',');
            }
       }
    I am unfamiliar with the functions in the string library so I don't know the best way to approach this. Also the code about I haven't actually tested it was just off the top of my head.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think you mean data not char at line 13.

    For converting strings of ASCII digits to binary values look up

    strtol

    or

    atoi

    In a standard library reference (or Google)

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Read the Insight article on the State Design Pattern. Your string parse is already coded for you as the example uses in the article. Just modify the code to break on a comma rather than a space.

      That will fetch the tokens as a string with the values in it.

      Then I would write a function taking a string& as an argument and returning an int. Just define a reverse_iterato r and set it to the argument string rbegin(). Then just loop adding up the string element values and muliplying by 10 on each cycle of the loop. Return the sum.

      I would not use any C functions.

      Comment

      • dschu012
        New Member
        • Jul 2008
        • 39

        #4
        Nvm found the article. And when you say don't use any see functions does that mean use don't use anything like atoi() or strtol() like the first guy suggested. And ya Banfa I meant data on that one line.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          That's correct. atoi and strtol are C string functions.

          The string functions in C aren't useable in C++ since they don't work with the C++ string object.

          atoi converts a C string to an int. strtol converts a C stirng to a long int. But C++ does not use C strings so there you go again.

          Using C strings in C++ is usually done by C++ programmers who first learned how to use C strings in C and haven't been able to advance to using C++ string objects.

          Comment

          Working...