C++ 将字符 * 转换为 LPWSTR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6858524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert char * to LPWSTR
提问by Skeith
I am trying to convert a program for multibyte character to Unicode.
我正在尝试将多字节字符程序转换为 Unicode。
I have gone through the program and preceded the string literals with L
so they look like L"string"
.
我已经完成了该程序并在字符串文字之前使用,L
因此它们看起来像L"string"
.
This has worked but I am now left with a C style string that won't conform. I have tried the L
and putting it in TEXT()
but the L
gets added to the variable name -- not the string -- if I use TEXT()
.
这已经奏效了,但我现在留下了一个不符合的 C 风格字符串。我已经尝试过L
并把它放进去,TEXT()
但是L
如果我使用TEXT()
.
I have tried making it a TCHAR
but then it complains that it cannot convert a TCHAR
to a char *
.
我曾尝试TCHAR
将其TCHAR
设为 a但随后它抱怨无法将 a 转换为 a char *
。
What options am I left with?
我还有哪些选择?
I know C and C++ are different. It is an old in-house C library that has been used in C++ projects for several years now.
我知道 C 和 C++ 是不同的。它是一个古老的内部 C 库,现已在 C++ 项目中使用了好几年。
回答by Raphael R.
The std::mbstowcs
function is what you are looking for:
该std::mbstowcs
功能是您正在寻找的功能:
char text[] = "something";
wchar_t wtext[20];
mbstowcs(wtext, text, strlen(text)+1);//Plus null
LPWSTR ptr = wtext;
for string
s,
对于string
s,
string text = "something";
wchar_t wtext[20];
mbstowcs(wtext, text.c_str(), text.length());//includes null
LPWSTR ptr = wtext;
--> ED: The "L" prefix only works on string literals, not variables. <--
--> ED:“L”前缀仅适用于字符串文字,不适用于变量。<--
回答by Kerrek SB
The clean way to use mbstowcs
is to call it twice to find the length of the result:
干净的使用方法mbstowcs
是调用它两次以找到结果的长度:
const char * cs = <your input char*>
size_t wn = mbsrtowcs(NULL, &cs, 0, NULL);
// error if wn == size_t(-1)
wchar_t * buf = new wchar_t[wn + 1](); // value-initialize to 0 (see below)
wn = mbsrtowcs(buf, &cs, wn + 1, NULL);
// error if wn == size_t(-1)
assert(cs == NULL); // successful conversion
// result now in buf, return e.g. as std::wstring
delete[] buf;
Don't forget to call setlocale(LC_CTYPE, "");
at the beginning of your program!
不要忘记setlocale(LC_CTYPE, "");
在程序开始时调用!
The advantage over the Windows MultiByteToWideChar
is that this is entirely standard C, although on Windows you might prefer the Windows API function anyway.
相对于 Windows 的优势MultiByteToWideChar
在于它完全是标准的 C,尽管在 Windows 上您可能更喜欢 Windows API 函数。
I usually wrap this method, along with the opposite one, in two conversion functions string
->wstring
and wstring
->string
. If you also add trivial overloads string
->string
and wstring
->wstring
, you can easily write code that compiles with the Winapi TCHAR
typedef in any setting.
我通常将此方法与相反的方法一起包装在两个转换函数string
->wstring
和wstring
-> 中string
。如果还添加了普通重载string
->string
和wstring
-> wstring
,则可以轻松编写TCHAR
在任何设置中使用 Winapi typedef 进行编译的代码。
[Edit:] I added zero-initialization to buf
, in case you plan to use the C array directly. I would usually return the result as std::wstring(buf, wn)
, though, but do beware if you plan on using C-style null-terminated arrays.[/]
[编辑:] 我添加了零初始化buf
,以防您打算直接使用 C 数组。std::wstring(buf, wn)
不过,我通常会将结果返回为,但如果您打算使用 C 风格的空终止数组,请务必小心。[/]
In a multithreaded environment you should pass a thread-local conversion state to the function as its final (currently invisible) parameter.
在多线程环境中,您应该将线程本地转换状态作为函数的最终(当前不可见)参数传递给函数。
Here is a small rantof mine on this topic.
这是我对这个话题的一个小小的咆哮。
回答by David Heffernan
This version, using the Windows API function MultiByteToWideChar()
, handles the memory allocation for arbitrarily long input strings.
此版本使用 Windows API 函数MultiByteToWideChar()
处理任意长输入字符串的内存分配。
int lenA = lstrlenA(input);
int lenW = ::MultiByteToWideChar(CP_ACP, 0, input, lenA, NULL, 0);
if (lenW>0)
{
output = new wchar_t[lenW];
::MultiByteToWideChar(CP_ACP, 0, input, lenA, output, lenW);
}
回答by Ajay
You may use CString
, CStringA
, CStringW
to do automatic conversions and convert between these types. Further, you may also use CStrBuf
, CStrBufA
, CStrBufW
to get RAII pattern modifiable strings
您可以使用CString
,CStringA
,CStringW
做自动转换和这些类型之间的转换。此外,您也可以使用CStrBuf
,CStrBufA
,CStrBufW
获得RAII模式修改字符串
回答by MoraRockey
I'm using the following in VC++ and it works like a charm for me.
我在 VC++ 中使用以下内容,它对我来说就像一个魅力。
CA2CT(charText)