拨开字符编码的迷雾--字符编码转换
来源:互联网 发布:php print echo 区别 编辑:程序博客网 时间:2024/05/16 12:54
拨开字符编码迷雾系列文章链接:
- 拨开字符编码的迷雾--字符编码概述
- 拨开字符编码的迷雾--编译器如何处理文件编码
- 拨开字符编码的迷雾--字符编码转换
1. API介绍
本文介绍使用Windows API进行字符编码的转换,涉及WideCharToMultiByte
和MultiByteToWideChar
2个API,
API接口名中的MultiByte
对应着多字节编码,如ASCII、UTF-8等都是多字节编码,而WideChar
字面意思是宽字符,在windows内部宽字符特指UTF-16编码。 原型如下:
int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar );
int MultiByteToWideChar( UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar );
2. 接口封装
std::string UnicodeToANSI(const std::wstring &str, UINT iCodePage = CP_ACP) { std::string strRes; int iSize = ::WideCharToMultiByte(iCodePage, 0, str.c_str(), -1, NULL, 0, NULL, NULL); if(iSize == 0) return strRes; char *szBuf = new (std::nothrow) char[iSize]; if(!szBuf) return strRes; memset(szBuf, 0, iSize); ::WideCharToMultiByte(iCodePage, 0, str.c_str(), -1, szBuf, iSize, NULL, NULL); strRes = szBuf; delete[] szBuf; return strRes;}std::wstring ANSIToUnicode(const std::string &str, UINT iCodePage = CP_ACP) { std::wstring strRes; int iSize = ::MultiByteToWideChar(iCodePage, 0, str.c_str(), -1, NULL, 0); if(iSize == 0) return strRes; wchar_t *szBuf = new (std::nothrow) wchar_t[iSize]; if(!szBuf) return strRes; memset(szBuf, 0, iSize * sizeof(wchar_t)); ::MultiByteToWideChar(iCodePage, 0, str.c_str(), -1, szBuf, iSize); strRes = szBuf; delete[] szBuf; return strRes;}std::string UnicodeToUTF8(const std::wstring &str) { std::string strRes; int iSize = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL); if(iSize == 0) return strRes; char *szBuf = new (std::nothrow) char[iSize]; if(!szBuf) return strRes; memset(szBuf, 0, iSize); ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, szBuf, iSize, NULL, NULL); strRes = szBuf; delete[] szBuf; return strRes;}std::string UnicodeToUTF8BOM(const std::wstring &str) { std::string strRes; int iSize = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL); if (iSize == 0) return strRes; unsigned char *szBuf = new (std::nothrow) unsigned char[iSize + 3]; if (!szBuf) return strRes; memset(szBuf, 0, iSize + 3); if (::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, (LPSTR)(szBuf + 3), iSize, NULL, NULL) > 0) { szBuf[0] = 0xEF; szBuf[1] = 0xBB; szBuf[2] = 0xBF; } strRes = (char*)szBuf; delete[] szBuf; return strRes;}std::wstring UTF8ToUnicode(const std::string &str) { std::wstring strRes; int iSize = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); if(iSize == 0) return strRes; wchar_t *szBuf = new (std::nothrow) wchar_t[iSize]; if(!szBuf) return strRes; memset(szBuf, 0, iSize * sizeof(wchar_t)); ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, szBuf, iSize); strRes = szBuf; delete[] szBuf; return strRes;}std::string ANSIToUTF8(const std::string &str) { return UnicodeToUTF8(ANSIToUnicode(str));}std::string ANSIToUTF8BOM(const std::string &str) { return UnicodeToUTF8BOM(ANSIToUnicode(str));}std::string UTF8ToANSI(const std::string &str) { return UnicodeToANSI(UTF8ToUnicode(str));}
对于只支持简体中文(部分韩文、日文)的系统,iCodePage
可以使用CP_ACP,这时API会使用系统当前的代码页(简体中文系统为CP936,即GBK字符集)来进行编码转换。 但遇到如下情况就需要手动指定代码页了:
- 需要转换的字符串中的文字是系统当前代码页不支持的。如字符串中含有中文,而当前系统代码页确是英文的;
- GBK字符集中只包含了一部分韩文和日文,部分韩文和日文的转换可以正常转换,若遇到不能转换的情况也需要将指定iCodePage为特定的支持韩文或日文的代码页了,特别是中文和韩文、日文等混合的情况下。如韩文“탉”不包含在GBK中,若这时仍然使用CP_ACP就会得到错误的转换结果
?
,十六进制3F
。但GB18030(代码页为54936)支持“탉”,可以手动指定iCodePage为54936。
如果代码中含有GBK不支持的字符,如“탉”、“
阅读全文
0 0
- 拨开字符编码的迷雾
- 拨开字符编码的迷雾
- 拨开字符编码的迷雾--字符编码转换
- 拨开字符编码的迷雾--字符编码转换
- 拨开字符编码的迷雾--字符编码转换
- 拨开字符编码的迷雾--MySQL数据库字符编码
- 拨开字符编码的迷雾--MySQL数据库字符编码
- 拨开字符编码的迷雾--MySQL数据库字符编码
- 拨开字符编码的迷雾--字符编码概述
- 拨开字符编码的迷雾--MySQL数据库字符编码
- 拨开字符编码的迷雾--编译器如何处理文件编码
- 拨开字符编码的迷雾--编译器如何处理文件编码
- 拨开字符编码的迷雾--编译器如何处理文件编码
- 拨开字符编码的迷雾--编译器如何处理文件编码
- 字符编码之间的转换
- QT的字符编码转换 .
- 字符编码的转换iconv
- QT的字符编码转换
- 算法笔记:树和二叉树基础
- 使用weex,开发美食app之vue页面实现
- idea 创建dubbo服务 jar包形式
- Codeforces Round #432 (Div. 1): B. Arpa and a list of numbers
- Python中len()和range()函数
- 拨开字符编码的迷雾--字符编码转换
- 机器学习五大流派,九种常见算法优点场景举例
- css常用效果总结
- DOS黑窗口被选定导致tomcat项目访问不到
- composer windows安装,使用新手入门
- 剑指Offer_面试题41_和为s的两个数字 VS 和为s的连续正数序列
- RxTools一款强大实用的工具类集合
- 《Spring3实战》摘要(6)事务管理
- Unity陀螺仪功能