我正在尝试实现将 char* 转换为 wchar_t* 的函数。但问题是,wprintf 显示不同的结果。我做错了什么?
wchar_t *toWchar(char *data)
{
if(!data)
{
return NULL;
}
int size = strlen(data);
if(!size)
{
return NULL;
}
char *temp = (char *)malloc(size * 2);
if(!temp)
{
return NULL;
}
int j = 0;
for(int i = 0; i < size; i++)
{
temp[j++] = data[i];
temp[j++] = '\0';
}
return (wchar_t *)temp;
}
编辑: 主要功能:
int main()
{
wchar_t *temp = toWchar("hello, world!");
if(temp)
wprintf("%ls\n", temp);
return 0;
}
最佳答案
作为起点; gcc 将为您提供平台相关的 wchar 类型/大小,如下所示:
echo "" | gcc -E - -dM | grep WCHAR
#define __WCHAR_MAX__ 2147483647
#define __WCHAR_MIN__ (-__WCHAR_MAX__ - 1)
#define __GCC_ATOMIC_WCHAR_T_LOCK_FREE 2
#define __WCHAR_TYPE__ int
#define __SIZEOF_WCHAR_T__ 4
A resource建议:
“C 和 C++ 在其各自标准的 2011 年修订版中引入了固定大小的字符类型 char16_t 和 char32_t,以提供 16 位和 32 位 Unicode 转换格式的明确表示,而 wchar_t 则由实现定义。”
关于c++ - char * 到 wchar_t * 函数实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17913500/