Connecting Tech Pros Worldwide Forums | Help | Site Map

Split string into a char array

Newbie
 
Join Date: Jul 2008
Posts: 28
#1: 3 Weeks Ago
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,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"

this is the output that I would want

char data[4][8] =
{0x0,0x0,0x0,0x0,0xf0,0xf0,0xf0,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

Expand|Select|Wrap|Line Numbers
  1. 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";
  2. char data[4][8];
  3. int i,j;
  4. char result;
  5. char * tok;
  6.  
  7. tok = strtok(s,',');
  8. for(i=0;i<4;i++)
  9.    for(j=0;j<8;j++)
  10.    {
  11.       if(tok != NULL) {
  12.          //convert tok into it's value as a char somehow and store it in result
  13.          char[i][j] = result;
  14.          tok = strtok(NULL,',');
  15.         }
  16.    }
  17.  
  18.  
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's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,208
#2: 3 Weeks Ago

re: Split string into a char array


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)
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#3: 3 Weeks Ago

re: Split string into a char array


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_iterator 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.
Newbie
 
Join Date: Jul 2008
Posts: 28
#4: 3 Weeks Ago

re: Split string into a char array


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.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#5: 3 Weeks Ago

re: Split string into a char array


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.
Reply