Programming Question c++: How to add a number after a \0 in a char array?

Topper

Addon Developer
Addon Developer
Donator
Joined
Mar 28, 2008
Messages
666
Reaction score
20
Points
33
Hello,
i want to parse a connected char array to an array of strings.
In the char array, '\0' is used to seperate the string.
After the '\0' is a number, in this case 456.
The problem is that \0456 will be replaced by the assci sign 456.
So how I can initialize the char array right instead of this?:






Background:


Code:
char charArray[]  = "ABC\0456\0"
void getStringArraysFromCharArray()
{  
  
  int n_woerter = 0;
  for (int i=0; i<sizeof(charArray); i++)
  {
    if (charArray[i] == '\0') n_woerter++;
  }
  String *woerter = new String[n_woerter];
  int d_ptr = 0;
  
  for (int i=0; i<n_woerter; i++)
  {
    woerter[i].concat(&charArray[d_ptr]);
    d_ptr += woerter[i].length() + 1;
  }
...
}
 
Last edited:

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,616
Reaction score
2,336
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
You mean: Not escaping the number with the \nnnnn? What about using a \u0000 there instead of a \0?

\0456 is not turned into unicode, btw, but interpreted as octal byte. How the result looks like depends on the compiler though, since you are using a value too large for a byte in that example. \x00 would also work maybe, but again, you could get compiler specific issues, since \x00456 could still be a legal 32bit character...

Or alternative: Use a different delimiter if allowed.
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
2,064
Reaction score
507
Points
113
I would let the preprocessor's string-literal concatenation work for you:
PHP:
char foo[] = "Hello" "-" "World"; // equals: "Hello-World" (+ terminating zero)
so for your casse
PHP:
char charArray[] = "ABC\0" "456\0";
The 2nd "\0" (after your three-digit number) is redundant however,
but it is needed in case you don't know the array size at compile-time.
Then a "terminating-zero" indicates a word delimiter and the "double-terminating-zero" indicates the end of list. Microsoft Foundation Classes (MFC) resources used this technique a lot in list initializers (.rc files).

---------- Post added at 21:46 ---------- Previous post was at 21:44 ----------

Memory looks like this then:

41 42 43 00 34 35 36 00 00 | ABC.456..


---------- Post added at 21:49 ---------- Previous post was at 21:46 ----------

The preprocessor's string-literal concatenation also has the nice benefit of letting you do a more "logical" formatting of your words, like:
PHP:
char input[] =
  "First\0"
  "Second\0"
  "3rd\0"
  "last";
 
Last edited:

Topper

Addon Developer
Addon Developer
Donator
Joined
Mar 28, 2008
Messages
666
Reaction score
20
Points
33
@Urwumpe, if I replace the delimiter by another one, String.concat would not work anymore...
I also tried the other solutions, they also not work, because i gues e.g. "\0x00456" is also a valid hex code or isnt it?



kuddel that solution works.


Thank you!
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
2,064
Reaction score
507
Points
113
And while we're at it, I would do the splitting a little different:
PHP:
#include<vector>

char charArray[] = "ABC\0" "456\0"; // the last zero is *ESSENTIAL* here!

void splitStringList ()
{
	std::vector<std::string> words;
	const char* p = charArray;
	do {
		words.push_back(std::string(p));
		p += words.back().size() + 1;
	} while (*p != 0);
	// ...
}
 
Last edited:
Top