GBK与UTF-8的相互转化 Windows Linux
来源:互联网 发布:edrawmax mac 注册码 编辑:程序博客网 时间:2024/05/01 15:30
在程序中经常用到GBK与UTF-8的转化,尤其是对于一些中文字符的处理,现总结如下:
windows:
std::string UTF8ToGBK(const std::string& strUtf8)//UTF-8编码转化为多字节
{
std::string strGBK;
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8.c_str(), -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8.c_str(), -1, (LPWSTR)wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL,NULL);
strGBK=szGBK;
delete[] szGBK;
delete[] wszGBK;
return strGBK;
}
std::string GBKToUTF8(const std::string& strGBK)
{
std::string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete[]str1;
str1 = NULL;
delete[]str2;
str2 = NULL;
return strOutUTF8;
}
linux:
int GbkToUtf8(char *sOut, int iMaxOutLen, const char *sIn, int iInLen)
{
char *pIn = (char *)sIn;
char *pOut = sOut;
size_t ret;
size_t iLeftLen=iMaxOutLen;
iconv_t cd;
cd = iconv_open("utf-8","gbk");
if (cd == (iconv_t) - 1)
{
return -1;
}
size_t iSrcLen=iInLen;
ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen);
if (ret == (size_t) - 1)
{
iconv_close(cd);
return -1;
}
iconv_close(cd);
return (iMaxOutLen - iLeftLen);
}
int Utf8ToGbk(char *sOut, int iMaxOutLen, const char *sIn, int iInLen)
{
char *pIn = (char *)sIn;
char *pOut = sOut;
size_t ret;
size_t iLeftLen=iMaxOutLen;
iconv_t cd;
cd = iconv_open("gbk","utf-8");
if (cd == (iconv_t) - 1)
{
return -1;
}
size_t iSrcLen=iInLen;
ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen);
if (ret == (size_t) - 1)
{
iconv_close(cd);
return -1;
}
iconv_close(cd);
return (iMaxOutLen - iLeftLen);
}
- GBK与UTF-8的相互转化 Windows Linux
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- GBK与UTF-8转化
- linux和windows下 unicode、GBK、utf-8 互相转化
- 将windows下的GBK编码格式文件转化为Linux下UTF-8编码格式文件
- GBK和UTF-8的区别与相互转换
- GBK与UTF-8 之间的相互转换
- C# UTF-8与GB2312编码的相互转化
- 字符串编码转换(GBK) NSString, char, NSData相互转化 从 GBK 转到 UTF-8
- linux utf-8 windows gbk eclipse乱码
- utf-8与gbk相互转换c++实现
- 数据结构第二天
- Django1.10 release notes摘编
- C的函数,变量声明,malloc,MessageBox,sprintf,简单应用
- 02、CSS3结构选择器
- Google Play
- GBK与UTF-8的相互转化 Windows Linux
- Ice_cream's world I<hdoj2120>
- Windows下用cmd命令安装及卸载服务
- 用matlab实现自定义函数的数据拟合
- WCF 客户端调用几种方式
- vs2010无法显示Dev的工具
- poj 1062 昂贵的聘礼
- Is It A Tree?<hdoj1325>
- 练习题