转换Unicode字符串,反之亦然 [英] Converting unicode strings and vice-versa
问题描述
我是一个新的使用 Unicode 字符串和指针,我不知道如何转换为unicode到ascii和反之亦然的工作原理。以下是我想要做的,
I'm kind of new to using Unicode string and pointers and I've no idea how the conversion to unicode to ascii and versa-versa works. Following is what I'm trying to do,
const wchar_t *p = L"This is a string";
如果我想将其转换为 char *
,转换如何使用转换 wchar_t *
到 char *
,反之亦然吗?
If I wanted to convert it to char*
, how would the conversion work with converting wchar_t*
to char*
and vice-versa?
或通过使用 wstring
到 string
/ p>
or by value using wstring
to string
class object and vice-versa
std::wstring wstr = L"This is a string";
如果我是正确的,你可以将字符串复制到一个新的缓冲区, p>
If i'm correct, can you just copy the string to a new buffer without conversion?
推荐答案
解决方案是平台相关的。在Windows上,使用 MultiByteToWideChar 和 WideCharToMultiByte API函数。在Unix / linux平台上, iconv 库非常受欢迎。
The solutions are platform-dependent. On Windows use MultiByteToWideChar and WideCharToMultiByte API functions. On Unix/linux platforms iconv library is quite popular.
这篇关于转换Unicode字符串,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!