64,654
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
char s[]="%E6%B1%89%E5%AD%97%E7%BC%96%E7%A0%81";
int len = strlen(s);
char* temps = new char[len+1];
temps[len] = '\0';
int temps_pos = 0;
int i;
//去掉%号
for ( i = 0;i<len;++i)
{
if (s[i] != '%')
{
temps[temps_pos] = s[i];
temps_pos++;
}
}
temps[temps_pos] = '\0';
cout<<temps<<endl;
int temps_len = strlen(temps);
int UTF8len = temps_len/2;
char* UTF8str = new char[UTF8len+1];
UTF8str[UTF8len] = '\0';
//把字符转换成数字,得到真实的UTF8串
for ( i = 0;i<UTF8len*2;)
{
char convert[3] = {0};
convert[0] = temps[i++];
convert[1] = temps[i++];
char *end;
int tempint = strtol(convert,&end,16);
UTF8str[i/2-1] = tempint;
}
//UTF8转换到UTF16
int wcslen = ::MultiByteToWideChar(CP_UTF8,NULL,UTF8str,UTF8len,NULL,0);
wchar_t* wszString = new wchar_t[wcslen + 1];
::MultiByteToWideChar(CP_UTF8,NULL,UTF8str ,UTF8len,wszString,wcslen);
wszString[wcslen] = L'\0';
//设置区域
std::wcout.imbue(std::locale("CHS"));
wcout<<wszString<<endl;
delete []temps;
delete []UTF8str;
delete []wszString;
return 0;
}