LInux下使用mbstowcs函数可以将char转化为wchar_t
函数含义:convert a multibyte string to a wide char string
说明: The behaviour of mbstowcs depends on the LC_CTYPE category of the current locale
返回值: The mbstowcs() function returns the number of wide characters that make up the converted part of the wide-char-acter string, not including the terminating null wide character. If an invalid multibyte sequence was encountered, (size_t) -1 is returned.
注意:wcout 与cout不要混合使用,否则会导致wchar_t的输出问题
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
#include <iostream>
using namespace std;
int ToWchar(char* &src, wchar_t* &dest, const char *locale = "zh_CN.utf8")
{
if (src == NULL) {
dest = NULL;
return 0;
}
setlocale(LC_CTYPE, locale);
int w_size = mbstowcs(NULL, src, 0) + 1;
if (w_size == 0) {
dest = NULL;
return -1;
}
wcout << "w_size" << w_size << endl;
dest = new wchar_t[w_size];
if (!dest) {
return -1;
}
int ret = mbstowcs(dest, src, strlen(src)+1);
if (ret <= 0) {
return -1;
}
return 0;
}
int main()
{
char* str = "中国123";
wchar_t *w_str ;
ToWchar(str,w_str);
wcout << w_str[0] << "--" << w_str[1] << "--" << w_str[2];
delete(w_str);
return 0;
}
————————————————
原文链接:https://blog.csdn.net/caigen1988/article/details/19075073
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)