storing 0x0 in strings

Red Squirrel

No Lifer
May 24, 2003
70,188
13,578
126
www.anyf.ca
Is it possible to store 0x0 in strings as regular data? or is this used for null terminator? I wrote this simple function for use in networking:

string ForceSize(string str,int size,char fillerchar)
{
if(str.size()>size)return str.substr(0,size);

int charstoadd=size-str.size();

char * filler = new char[charstoadd];

for(int i=0;i<charstoadd;i++)filler=fillerchar;

string temp = str + filler;

delete filler;

return temp;
}

If fillerchar is 0x0 (which will be the case in networking usually) the function does not seem to work. My guess is that it sees the first one and figueres its the end of the string.

Is there a way to get this to work? Or is the only way to send it directly? I want to be able to use this helper function as well as others to format a string, then send it all as one buffer. Would I get better luck with char arrays maybe? I don't want to revert to those, but I supose for networking its not really the end of the world as packets will only go up to a certain size so I can live with using char arrays.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I believe you could use a wide byte / multichar string. I'm not quite sure how this works but I remember seeing a program on SysInternals that wrote NULL chars to the registry using unicode, multichar, confusing conventional ASCII programs that read the registry. There's probably a special NUL char you can use.

Search for NUL here: http://en.wikipedia.org/wiki/UTF-8

Edit: sorry, I can't remember the specifics. It's possible unicode/multi char had nothing to do with it. What you can do is specify a certain size for the string and use string functions that deal with a certain number of characters instead of looking for null terminators.
 

Red Squirrel

No Lifer
May 24, 2003
70,188
13,578
126
www.anyf.ca
Actually I just found a way. It DOES work as far as I know, now. It was just the way I was adding the char, it was not going through for some reason. Using append seems to work.

Code:
string ForceSize(string str,int size,char fillerchar)
{
if(str.size()>size)return str.substr(0,size);

int charstoadd=size-str.size();

[b]for(int i=0;i<charstoadd;i++)str.append(1,fillerchar);[/b]

return str;
}
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Ah somehow I forgot you were using the C++ string class (MS?). I'm not even sure how those work, so I couldn't help you.

The other way probably didn't work because you were adding "nothing" to the string. When it overloaded the + operator, it was probably looking for a null-terminated string. So, effectively, it added nothing. With this method, it looks like you're specifying the number of characters.
 

engineereeyore

Platinum Member
Jul 23, 2005
2,070
0
0
Doesn't look like you're using it anymore, but shouldn't your loop be

for(int i = 0; i<charstoadd; i++) filler = fillerchar;?
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
You should also be able to change filler to be of type String and have it work. The String class keeps a count of the number of characters in it to determine the end point it. There is no way for a C program to know how long an array is so the null char is used for termination.

The size of a pointer is keep around for freeing the memory but the size of memory is >= to size of the array.