C++ 如何声明 wchar_t 并稍后设置其字符串值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19070868/
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
How to declare wchar_t and set its string value later on?
提问by user780756
I am developing for Windows, I have not found adequate information on how to correctly declare and later on set a unicode string. So far,
我正在为 Windows 开发,我还没有找到关于如何正确声明和稍后设置 unicode 字符串的足够信息。迄今为止,
wchar_t myString[1024] = L"My Test Unicode String!";
What I assumethe above does is [1024] is the allocated string length of how many characters I need to have max in that string. L"" makes sure the string in quotes is unicode (An alt I found is _T()). Now later on in my program when I am trying to set that string to another value by,
我假设上面所做的是 [1024] 是分配的字符串长度,即我需要在该字符串中拥有多少个字符的最大值。L"" 确保引号中的字符串是 unicode(我发现的一个 alt 是 _T())。现在稍后在我的程序中,当我尝试将该字符串设置为另一个值时,
myString = L"Another text";
I get compiler errors, what am I doing wrong?
我收到编译器错误,我做错了什么?
Also if anyone has an easy and in-depth unicode app resource I'd like to have some links, used to have bookmarked a website which was dedicated to that but seems that now is gone.
此外,如果有人有一个简单而深入的 unicode 应用程序资源,我想有一些链接,曾经为一个专门用于此的网站添加了书签,但现在似乎已经消失了。
EDIT
编辑
I provide the entire code, I intend to use this as a DLL function but nothing so far is returned.
我提供了完整的代码,我打算将其用作 DLL 函数,但到目前为止没有返回任何内容。
#include "dll.h"
#include <windows.h>
#include <string>
#include <cwchar>
export LPCSTR ex_test()
{
wchar_t myUString[1024];
std::wcsncpy(myUString, L"Another text", 1024);
int myUStringLength = lstrlenW(myUString);
MessageBoxW(NULL, (LPCWSTR)myUString, L"Test", MB_OK);
int bufferLength = WideCharToMultiByte(CP_UTF8, 0, myUString, myUStringLength, NULL, 0, NULL, NULL);
if (bufferLength <= 0) { return NULL; } //ERROR in WideCharToMultiByte
return NULL;
char *buffer = new char[bufferLength+1];
bufferLength = WideCharToMultiByte(CP_UTF8, 0, myUString, myUStringLength, buffer, bufferLength, NULL, NULL);
if (bufferLength <= 0) { delete[] buffer; return NULL; } //ERROR in WideCharToMultiByte
buffer[bufferLength] = 0;
return buffer;
}
回答by Dietmar Kühl
The easiest approach is to declare the string differently in the first place:
最简单的方法是首先以不同的方式声明字符串:
std::wstring myString;
myString = L"Another text";
If you insist in using arrays of wchar_t
directly, you'd use wcscpy()
or better wcsncpy()
from <cwchar>
:
如果您在使用数组坚持wchar_t
直接,你会使用wcscpy()
或更好wcsncpy()
的<cwchar>
:
wchar_t myString[1024];
std::wcsncpy(myString, L"Another text", 1024);
回答by Kal
wchar_t myString[1024] = L"My Test Unicode String!";
is initializing the array like this
正在像这样初始化数组
wchar_t myString[1024] = { 'M', 'y', ' ', ..., 'n', 'g', '!', 'myString = L"Another text";
' };
but
但
const auto& newstring = L"Another text";
std::copy(std::begin(newstring), std::end(newstring), myString);
is an assignment which u cannot do to arrays. u have to copy the contents of the new string into your old array:
是您不能对数组进行的分配。您必须将新字符串的内容复制到旧数组中:
wchar_t* newstring = L"Another text";
std::copy(newstring, newstring + wsclen(newstring) + 1, myString);
or if its a pointer
或者如果它是一个指针
std::copy_n(newstring, wsclen(newstring) + 1, myString);
or as nawaz suggested with copy_n
或者像 nawaz 建议的那样 copy_n