• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

storing 0x0 in strings

Red Squirrel

No Lifer
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.
 
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.
 
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;
}
 
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.
 
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.
 
Back
Top