马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Crez.晔霖 于 2020-3-19 11:15 编辑
来分享一下自己写的各类编码转换,大部分测试可用,部分没有测试,当然适量参考了一下网络资源哈
废话不多说,上代码:
Unicode转ANSIwchar_t* AnsiToUnicode(std::string AnsiString)
{
char* AnsiByte = const_cast<char*>(AnsiString.c_str());
int Length = MultiByteToWideChar(CP_ACP, 0, AnsiByte, -1, 0, 0);
wchar_t* Result = new wchar_t[Length * 2];
MultiByteToWideChar(936, 0, AnsiByte, -1, Result, Length * 2);
Result += '\0';
return Result;
}
ANSI转Unicodestd::string UnicodeToAnsi(const wchar_t* UnicodeByte)
{
int Length = WideCharToMultiByte(CP_ACP, 0, UnicodeByte, -1, 0, 0, 0, 0);
char* ResultByte = new char[Length * 2];
WideCharToMultiByte(CP_ACP, 0, UnicodeByte, -1, ResultByte, Length, 0, 0);
ResultByte += '\0';
return ResultByte;
}
GB2312转Unicodewchar_t* GB2312ToUnicode(std::string GB2312String)
{
char* GB2312Byte = const_cast<char*>(GB2312String.c_str());
int Length = MultiByteToWideChar(936, 0, GB2312Byte, -1, 0, 0);
wchar_t* ResultByte = new wchar_t[Length * 2];
MultiByteToWideChar(936, 0, GB2312Byte, -1, ResultByte, Length * 2);
return ResultByte;
}
Unicode转GB2312std::string UnicodeToGB2312(wchar_t* UnicodeByte)
{
int Length = WideCharToMultiByte(936, 0, UnicodeByte, -1, 0, 0, 0, 0);
char* ResultByte = new char[Length * 2];
WideCharToMultiByte(936, 0, UnicodeByte, -1, ResultByte, Length, 0, 0);
ResultByte += '\0';
return ResultByte;
}
UTF8转Unicodewchar_t* UTF8ToUnicode(std::string UTF8String)
{
char* UTF8Byte = const_cast<char*>(UTF8String.c_str());
int Length = MultiByteToWideChar(65001, 0, UTF8Byte, -1, 0, 0);
wchar_t* ResultByte = new wchar_t[Length * 2];
MultiByteToWideChar(65001, 0, UTF8Byte, -1, ResultByte, Length);
return ResultByte;
}
Unicode转UTF8std::string UnicodeToUTF8(wchar_t* UnicodeByte)
{
int Length = WideCharToMultiByte(65001, 0, UnicodeByte, -1, 0, 0, 0, 0);
char* ResultByte = new char[Length * 2];
WideCharToMultiByte(65001, 0, UnicodeByte, -1, ResultByte, Length, 0, 0);
ResultByte += '\0';
return ResultByte;
}
备注:想要把[wchat_t*]转换成[char*]或[unsigned char*]直接在前面加(char*)或(unsigned char*)即可~
以后可能会补一些新的,谢谢支持~ |