Can i use string in C++ which support embedded NULL character?
My issue is: Constructing string with embedded NULL and hence sent it to C++ DLL as an array of bytes.
string inputStr("he\0llo", 6);
int byteLength = 6;
BYTE *inputByte = (BYTE*)(char*)inputStr.c_str();
ApplyArabicMapping(inputByte , byteLength);
Yes, std::string
supports storing NULL
characters because it is not NULL
-terminated. You can create one in many ways:
string str("he\0llo", 6);
str.append(1, '\0');
str.push_back('\0');
const char[] cstr = "hell\0o";
string str2(cstr, cstr + sizeof(cstr) - 1); // - 1 for the NULL
You can use counted strings , where the character buffer is stored alongside with its "content length"; this allows you to embed any character. std::string
, for example, is a kind of counted string.
Obviously, you cannot pass such a string to a function that expects a classic C-string, because it will see the first null it encounters as the string terminator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.