我们在使用字符串的时候会遇到各种各样的编码问题不胜其烦。
本例例举了一些windows下常用的字符集转换函数。方便使用
Unicode和Ascii的互转
//unicode转为ascii
std::string UnicodeToAscii( const std::wstring& in_str )
{
int nNeedChars = WideCharToMultiByte( CP_ACP, 0, in_str.c_str(), -1, 0, 0, 0, 0 );
if (nNeedChars > 0)//再次判断一下
{
std::string temp;
temp.resize(nNeedChars);
::WideCharToMultiByte( CP_ACP, 0, in_str.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
return temp;
}
return std::string();
}
//ascii转为unicode
std::wstring AsciiToUnicode( const std::string& in_str )
{
int nNeedWchars = MultiByteToWideChar( CP_ACP, 0, in_str.c_str(), -1, NULL, 0 );
if (nNeedWchars > 0)
{
std::wstring temp;
temp.resize(nNeedWchars);
::MultiByteToWideChar( CP_ACP, 0, in_str.c_str(), -1, &temp[0], nNeedWchars );
return temp;
}
return std::wstring();
}
Utf8和Unicode的互转
//utf8转为unicode
std::wstring UTF8ToUnicode( const std::string& in_utf8Str )
{
int nNeedWchars = MultiByteToWideChar( CP_UTF8, 0, in_utf8Str.c_str(), -1, NULL, 0 );
if (nNeedWchars > 0)
{
std::wstring temp;
temp.resize(nNeedWchars);
::MultiByteToWideChar( CP_UTF8, 0, in_utf8Str.c_str(), -1, &temp[0], nNeedWchars );
return temp;
}
return std::wstring();
}
//unicode转为utf8
std::string UnicodeToUTF8( const std::wstring& in_wStr )
{
int nNeedChars = WideCharToMultiByte( CP_UTF8, 0, in_wStr.c_str(), -1, 0, 0, 0, 0 );
if (nNeedChars > 0)//再次判断一下
{
std::string temp;
temp.resize(nNeedChars);
::WideCharToMultiByte( CP_UTF8, 0, in_wStr.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
return temp;
}
return std::string();
}
Ascii和Utf8的互转
//ascii转为utf8
std::string AsciiToUTF8(const std::string& in_asciiStr)
{
return UnicodeToUTF8(AsciiToUnicode(in_asciiStr));
}
//utf8转为ascii
std::string UTF8ToAscii(const std::string& in_utf8Str)
{
return UnicodeToAscii(UTF8ToUnicode(in_utf8Str));
}
GB2312和Unicode的互转
//GB2312 转换成 Unicode:
std::wstring GB2312ToUnicode(const std::string& strGB2312String)
{
UINT nCodePage = 936; //GB2312
int nNeedWchars = MultiByteToWideChar(nCodePage, 0, strGB2312String.c_str(), -1, NULL, 0);
if (nNeedWchars > 0)
{
std::wstring temp;
temp.resize(nNeedWchars);
::MultiByteToWideChar( nCodePage, 0, strGB2312String.c_str(), -1, &temp[0], nNeedWchars );
return temp;
}
return std::wstring();
}
//Unicode 转换成 GB2312:
std::string UnicodeToGB2312(const std::wstring& strUnicodeString)
{
UINT nCodePage = 936; //GB2312
int nNeedChars = WideCharToMultiByte(nCodePage, 0, strUnicodeString.c_str(), -1, NULL, 0, NULL, NULL);
//再次判断一下
if (nNeedChars > 0)
{
std::string temp;
temp.resize(nNeedChars);
::WideCharToMultiByte( nCodePage, 0, strUnicodeString.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
return temp;
}
return std::string();
}
BIG5和Unicode的互转
//BIG5 转换成 Unicode:
std::wstring BIG5ToUnicode(const std::string& strBIG5String)
{
UINT nCodePage = 950; //BIG5
int nNeedWchars = MultiByteToWideChar(nCodePage, 0, strBIG5String.c_str(), -1, NULL, 0);
if (nNeedWchars > 0)
{
std::wstring temp;
temp.resize(nNeedWchars);
::MultiByteToWideChar( nCodePage, 0, strBIG5String.c_str(), -1, &temp[0], nNeedWchars );
return temp;
}
return std::wstring();
}
//Unicode 转换成 BIG5:
std::string UnicodeToBIG5(const std::wstring& strUnicodeString)
{
UINT nCodePage = 950; //BIG5
int nNeedChars = WideCharToMultiByte(nCodePage, 0, strUnicodeString.c_str(), -1, NULL, 0, NULL, NULL);
//再次判断一下
if (nNeedChars > 0)
{
std::string temp;
temp.resize(nNeedChars);
::WideCharToMultiByte( nCodePage, 0, strUnicodeString.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
return temp;
}
return std::string();
}
GB2312和BIG5的互转
//繁体中文BIG5 转换成 简体中文 GB2312
std::string BIG5ToGB2312(const std::string& strBIG5String)
{
std::wstring strUnicode = BIG5ToUnicode(strBIG5String);
std::string strGB2312 = UnicodeToGB2312(strUnicode);
return strGB2312;
}
//简体中文 GB2312 转换成 繁体中文BIG5
std::string GB2312ToBIG5(const std::string& strGB2321String)
{
std::wstring strUnicode = GB2312ToUnicode(strGB2321String);
std::string strBIG5 = UnicodeToBIG5(strUnicode);
return strBIG5;
}
用法:
int _tmain(int argc, _TCHAR* argv[])
{
std::wstring wstr_unicode = L"你好吗";
std::string str_ascii = UnicodeToAscii(wstr_unicode);
std::string str_ascii_a = "你好吗ABC";
std::wstring str_unicode_a = AsciiToUnicode(str_ascii_a);
std::string str_utf8 = UnicodeToUTF8(wstr_unicode);
std::wstring str_unicode_b = UTF8ToUnicode(str_utf8);
std::string str_utf8_a = AsciiToUTF8(str_ascii_a);
std::string str_ascii_b = UTF8ToAscii(str_utf8_a);
std::string str_gb2312 = UnicodeToGB2312(wstr_unicode);
std::string str_big5 = UnicodeToBIG5(wstr_unicode);
std::string str_big5_a = GB2312ToBIG5(str_gb2312);
std::wstring str_unicode_c = BIG5ToUnicode(str_big5_a);
std::string str_gb2312_a = BIG5ToGB2312(str_big5_a);
return 0;
}