C++ 如何将 char* 转换为 wchar_t*?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8032080/
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 convert char* to wchar_t*?
提问by AutoBotAM
I've tried implementing a function like this, but unfortunately it doesn't work:
我试过实现这样的功能,但不幸的是它不起作用:
const wchar_t *GetWC(const char *c)
{
const size_t cSize = strlen(c)+1;
wchar_t wc[cSize];
mbstowcs (wc, c, cSize);
return wc;
}
My main goal here is to be able to integrate normal char strings in a Unicode application. Any advice you guys can offer is greatly appreciated.
我的主要目标是能够在 Unicode 应用程序中集成普通字符字符串。非常感谢你们提供的任何建议。
采纳答案by Cheers and hth. - Alf
Use a std::wstring
instead of a C99 variable length array. The current standard guarantees a contiguous buffer for std::basic_string
. E.g.,
使用 astd::wstring
而不是 C99 可变长度数组。当前标准保证 的连续缓冲区std::basic_string
。例如,
std::wstring wc( cSize, L'#' );
mbstowcs( &wc[0], c, cSize );
C++ does not support C99 variable length arrays, and so if you compiled your code as pure C++, it would not even compile.
C++ 不支持 C99 可变长度数组,因此如果您将代码编译为纯 C++,它甚至无法编译。
With that change your function return type should also be std::wstring
.
有了这个改变,你的函数返回类型也应该是std::wstring
.
Remember to set relevant locale in main
.
请记住在main
.
E.g., setlocale( LC_ALL, "" )
.
例如,setlocale( LC_ALL, "" )
。
Cheers & hth.,
干杯 & hth.,
回答by Andrew Shepherd
In your example, wc
is a local variable which will be deallocated when the function call ends. This puts you into undefined behavior territory.
在您的示例中,wc
是一个局部变量,它将在函数调用结束时被释放。这会让你进入未定义的行为领域。
The simple fix is this:
简单的修复是这样的:
const wchar_t *GetWC(const char *c)
{
const size_t cSize = strlen(c)+1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs (wc, c, cSize);
return wc;
}
Note that the calling code will then have to deallocate this memory, otherwise you will have a memory leak.
请注意,调用代码随后必须释放此内存,否则会出现内存泄漏。
回答by Maks
const char* text_char = "example of mbstowcs";
size_t length = strlen(text_char );
Example of usage "mbstowcs"
用法示例“mbstowcs”
std::wstring text_wchar(length, L'#');
//#pragma warning (disable : 4996)
// Or add to the preprocessor: _CRT_SECURE_NO_WARNINGS
mbstowcs(&text_wchar[0], text_char , length);
Example of usage "mbstowcs_s"
用法示例“mbstowcs_s”
Microsoft suggest to use "mbstowcs_s" instead of "mbstowcs".
Microsoft 建议使用“mbstowcs_s”而不是“mbstowcs”。
Links:
链接:
wchar_t text_wchar[30];
mbstowcs_s(&length, text_wchar, text_char, length);
回答by Greg Hewgill
You're returning the address of a local variable allocated on the stack. When your function returns, the storage for all local variables (such as wc
) is deallocated and is subject to being immediately overwritten by something else.
您正在返回分配在堆栈上的局部变量的地址。当您的函数返回时,所有局部变量(例如wc
)的存储空间将被释放,并且会立即被其他内容覆盖。
To fix this, you can pass the size of the buffer to GetWC
, but then you've got pretty much the same interface as mbstowcs
itself. Or, you could allocate a new buffer inside GetWC
and return a pointer to that, leaving it up to the caller to deallocate the buffer.
要解决这个问题,您可以将缓冲区的大小传递给GetWC
,但是您已经获得了与mbstowcs
它几乎相同的接口。或者,您可以在内部分配一个新缓冲区GetWC
并返回一个指向该缓冲区的指针,由调用者来释放缓冲区。
回答by Kerrek SB
Your problem has nothing to do with encodings, it's a simple matter of understanding basic C++. You are returning a pointer to a local variablefrom your function, which will have gone out of scope by the time anyone can use it, thus creating undefined behaviour(i.e. a programming error).
您的问题与编码无关,这是了解基本 C++ 的简单问题。您正在从您的函数返回一个指向局部变量的指针,当任何人都可以使用它时,该指针将超出范围,从而产生未定义的行为(即编程错误)。
Follow this Golden Rule: "If you are using naked char pointers, you're Doing It Wrong. (Except for when you aren't.)"
遵循这个黄金法则:“如果你使用裸字符指针,你就做错了。(除非你不是。)”
I've previously postedsome code to do the conversion and communicating the input and output in C++ std::string
and std::wstring
objects.
我之前已经发布了一些代码来进行转换并在 C++std::string
和std::wstring
对象中传达输入和输出。
回答by Gandalf458
I did something like this. The first 2 zeros are because I don't know what kind of ascii type things this command wants from me. The general feeling I had was to create a temp char array. pass in the wide char array. boom. it works. The +1 ensures that the null terminating character is in the right place.
我做了这样的事情。前 2 个零是因为我不知道这个命令想要我提供什么样的 ascii 类型的东西。我的总体感觉是创建一个临时字符数组。传入宽字符数组。繁荣。有用。+1 确保空终止字符在正确的位置。
char tempFilePath[MAX_PATH] = "I want to convert this to wide chars";
int len = strlen(tempFilePath);
// Converts the path to wide characters
int needed = MultiByteToWideChar(0, 0, tempFilePath, len + 1, strDestPath, len + 1);