3

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);
6
  • 2
    You'll have to be more clear than that... Commented Apr 4, 2012 at 11:11
  • why do you need that? the string class doesn't need the '\0' character to know that it ends. furthermore, if you use the string.c_str() method, you get an array of characters with the '\0' character at the end. Commented Apr 4, 2012 at 11:16
  • @alegen none of that changes anything. Commented Apr 4, 2012 at 11:21
  • Usually \0 indicates the end of a string. So if you pass c_str() with nulls in the middle to a function expecting a null terminated string it won't use the full string, just up to the first null. Maybe you need vector<char> instead. Why do you need this? Commented Apr 4, 2012 at 11:34
  • 1
    Downvoted: (It does not make that much of a difference to reputation) The answer is clearly incomplete, as the issue you are having is not with embedding a NUL character in a string (which in C++ would be std::string unless otherwise said), but rather in the use of a different interface. That is, the question lacks the most important bits of information, that are only sprinkled in comments. Commented Apr 4, 2012 at 12:04

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

Eventhough this is correct, i'd like to point out that std::vector<char> is functionally almost the same and has the advantage of making clear that you might find chars you don't expect in the container.
@RedX: I don't know, if it's still some kind of text string I'd still use std::string, while using std::vector<unsigned char> for binary data.
@RedX: you might find chars you don't expect in the container The problem with that statement is that it assumes that you should not expect a NUL in a std::string. I believe it is better to learn not to assume that and get over with. The difference between std::string and std::vector<char> is that the former indicates that you are storing text not just small numbers, and that it offers a much richer (too rich in places) for processing.
@AhmedMostafa: The problem is that you are trying to use a C string in the interface, and C strings don't allow for internal NUL characters (that and the casts have a funny smell, rethink them!). Because you cannot use NUL to determine the end of the string you will have to pass both the pointer to the first character and the length of the complete string (you seem to be doing that) and using the length inside the library ignoring the C string terminator. BTW, are you sure that your issue is not somewhere else? like the casts? (or in particular (BYTE), could that be BYTE*?)
@DavidRodríguez-dribeas One could argue that using std::vector<char> also says you're storing characters; I (and many others) would use std::vector<signed char> for small integers. Still, std::string does seem to suggest some sort of actual text, whereas std::vector<char> suggests just a lot of characters. I'd use std::string for text unless there were particular reasons not to, even though in practice, I never use anything that wouldn't work just as well with std::vector<char>.
2

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.

1 Comment

@AhmedMostafa maybe you should update the original question with your issue, as I (and possibly others) don't understand what you mean in the comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.