473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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,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.
Nov 5 '09 #1
4 8335
Banfa
9,065 Expert Mod 8TB
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)
Nov 5 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 5 '09 #3
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.
Nov 5 '09 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 6 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: andrewcw | last post by:
The split function takes as a parameter what I understand as array of Char. I can define the delimiter like this: string innerText = new string; char chSplit={'='};...
7
by: Christine | last post by:
My code has a split function that should split the text file of numbers. I've run this in previous programs as it is here and it worked, but now it wont work for some reason and returns...
4
by: Ori :) | last post by:
Hi guys I have a 50 charecters long string and I need to examine every charectar individually, I thought of converting the string into and Char array of 50 and then go over the array, how can I...
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
3
by: Maarten | last post by:
Hi all i'm trying to split a string without a separator example: 0011010 = s(1) = 0 s(2) = 0 s(3) = 1
12
by: garyusenet | last post by:
string lines = File.ReadAllLines(@"c:\text\history.txt"); foreach (string s in lines) { ArrayList results = new ArrayList(); string delimit = ";"; string currentline = s.Split(";"); ...
8
by: Robert | last post by:
in regards to parsing directories.. Dim Sep(0) as char Sep(0) = "\" Dim Parts() as string = Filename.Split(Sep) What I want is to declare a temporary variable that is passed to split...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.