最近整理了c/c++ 中各种字符,数组的转化,在此记录一下,如有问题错误还请指出。
/*
*日期:2019.10.16
*功能:多字符转16进制字符串(utf-8)
*参数说明:
cGbk[in]:多字符数组(gbk)
cUtf8[out]:十六进制字符串(utf-8)
*返回值:
*/
void GBKToUTF8(const char* cGbk, char* cUtf8)
{
int len = MultiByteToWideChar(936, 0, cGbk, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(936, 0, cGbk, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, cUtf8, len, NULL, NULL);
delete[] wstr;
}
/*
*日期:2019.10.16
*功能:宽字符转多字符
*参数说明:
cUtf8[in]:utf-8十六进制数组
cGbk[out]:多字节字符串(gbk)
*返回值:
*/
void UTF8ToGBK(const char* cUtf8, char* cGbk)
{
int len = MultiByteToWideChar(CP_UTF8, 0, cUtf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_UTF8, 0, cUtf8, -1, wstr, len);
len = WideCharToMultiByte(936, 0, wstr, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(936, 0, wstr, -1, cGbk, len, NULL, NULL);
delete[] wstr;
}