![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: string, wihtout l |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| the L character indicates that the string literal is a "wide string literal". In other words, an array of wchar_t rather than just plain old char. the 'wide char' is usually used for internationalisation, where you might want to use unicode instead of standard 1-byte characters. (Note - the size of wchar_t is not 'set in stone', though on most systems I'm aware of, it usually supports at least UTF-16) the C++ library has a whole bunch of wide character tools, all of which are similar to their 'narrow character' equivalents, but prefixed with a w. eg, wcout, wstring, wcin, etc. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (11-30-2007) | ||
| |||
| Not really - wide character strings aren't just a simple modification of adding L's to your program. you need to use the wide character string type and related I/O streams too. There's no point in storing simple strings (eg, "hello") in wide character format - since not only does it complicate things (wchar_t has no standard defined size, and its representations vary), it usually uses more bytes than necessary too. Internationalisation extends beyond wide characters into areas such as number representation, date representation, etc. The usual approach is to write the user interface portion separately, then only the UI will be concerned with these issues, allowing the rest of the program to continue internally without having to worry about how it looks to the outside world. If you want to make a habit of using wchar_t for everything, then feel free. although the more common approach is a conversion function to widen or narrow strings as you need to. eg, Code: #include <iostream>
#include <string>
#include <locale>
std::wstring widen_string(std::string str)
{
std::wstring result;
std::locale loc;
for(int i(0); i < str.size(); ++i)
{
result += std::use_facet<std::ctype<wchar_t> >(loc).widen(str[i]);
}
return result;
}
int main()
{
std::string str = "hello, world";
std::wcout << widen_string(str);
} Last edited by Bench : 12-01-2007 at 04:00 AM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (12-01-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |