Unicode Strings and the L prefix
-
String used in C++/CLI programs which are set to use Unicode character sets need not be prefixed with L. Is this an optional feature or is there something more to it? One thing that came to my notice is
String^ fileName = "primary" + '.' + "extn"; // primary46extn
and
String^ fileName = "primary" + L'.' + "extn"; // primary.extn
gave different results. So prefixing the dot with L, tells the compiler not to convert to integer.
« Superman »
-
String used in C++/CLI programs which are set to use Unicode character sets need not be prefixed with L. Is this an optional feature or is there something more to it? One thing that came to my notice is
String^ fileName = "primary" + '.' + "extn"; // primary46extn
and
String^ fileName = "primary" + L'.' + "extn"; // primary.extn
gave different results. So prefixing the dot with L, tells the compiler not to convert to integer.
« Superman »
Conversion from char to int is implicit in C++. In .NET, having a string and an int concatenated, automatically calls ToString on the int. so, C++ is turning your char to an int, and then .NET is trying to be helpful and turning that number into it's string representation.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Conversion from char to int is implicit in C++. In .NET, having a string and an int concatenated, automatically calls ToString on the int. so, C++ is turning your char to an int, and then .NET is trying to be helpful and turning that number into it's string representation.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
OK... Got that. For strings is the L prefix optional?
« Superman »
-
OK... Got that. For strings is the L prefix optional?
« Superman »
The ‘L’ prefix tells the complier to convert the [single byte] character sting into wide characters and is not optional, if the string type is expecting wide characters. Standard C++ examples:
std::string = “char - string”;
std::wstring = L”wchar_t – string”;
// ‘L’ prefix requiredchar* pStr = “char - string”;
wchar_t* pStr = L”wchar_t – string”;
// ‘L’ prefix requiredINTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
The ‘L’ prefix tells the complier to convert the [single byte] character sting into wide characters and is not optional, if the string type is expecting wide characters. Standard C++ examples:
std::string = “char - string”;
std::wstring = L”wchar_t – string”;
// ‘L’ prefix requiredchar* pStr = “char - string”;
wchar_t* pStr = L”wchar_t – string”;
// ‘L’ prefix requiredINTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
Thanks for the clarification. But I couldn't figure out how this works -
MessageBox::Show("Contents", L"Title");
The character set I'm using for the project is Unicode. You will notice that one parameter is with the 'L' prefix and the other is without it. So I'm guessing that the unicode version of
Show
is used and the 'L' prefix is optional.« Superman »
-
Thanks for the clarification. But I couldn't figure out how this works -
MessageBox::Show("Contents", L"Title");
The character set I'm using for the project is Unicode. You will notice that one parameter is with the 'L' prefix and the other is without it. So I'm guessing that the unicode version of
Show
is used and the 'L' prefix is optional.« Superman »
I have been away. From what little I have seen you appear to be using VC++ 2005, which I do not have. Therefore I can not test if this would even compile on that version of the compiler. The example you provided should generate errors, no matter what character set you are using. You can not mix Unicode and Multi-byte strings in a single function call, the function takes one or the other type, but not both. The only way this could work is if the compiler converted one of the strings, which it should not be doing, or the library provided functions that take mixed string argument types, which I have never seen before. In both these cases, MS would be doing something it should not be doing. Sorry I could not be of more help, all I know for sure is that the ‘L’ prefix is not optional and must be used to tell the compiler to convert the string to Unicode. Good Luck!
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra