跨平台字符编码转换GBK、UTF8
#if (defined _WIN32 || defined _WIN64)
# include <windows.h>
# include <stdio.h>
# include <ctype.h>
#elif defined(__linux__)
# include <iconv.h>
# include <wctype.h>
# include <wchar.h>
# include <errno.h>
#endif using namespace std; //代码页
#define CP_GBK 936
#define CP_UTF8 65001 std::wstring s2ws(const std::string str, int code_page);
std::string ws2s(const std::wstring wstr, int code_page); //默认的输出字符串字节长度
//经测试发现OUT_LEN = 10 每次可转3个汉字
const int OUT_LEN = ; /** @fn wstring s2ws(const string str, int code_page)
* @brief 从多字节字符串转为宽字符串
* @param str 源字符串
* @param code_page 要使用的代码页
* @return 成功返回宽字符串,失败返回空字符串
*/
wstring s2ws(const string str, int code_page)
{
wstring wstr_dest;
if (str.size() == )
{
return wstr_dest;
}
wchar_t* wcs = NULL;
#ifdef _MSC_VER
//要转换的多字节字符串
int size = MultiByteToWideChar(code_page, , str.c_str(), -, NULL, );
wcs = new(std::nothrow)wchar_t[size];
if (wcs == NULL)
{
return wstr_dest;
}
if (MultiByteToWideChar(code_page, , str.c_str(), -, wcs, size) == )
{
wstr_dest.clear();
}
else
{
wstr_dest += wcs;
}
delete[] wcs; #elif defined __linux
//申请临时缓冲区,用于保存转换后的字符串
wcs = new(std::nothrow)wchar_t[OUT_LEN];
if (wcs == NULL)
{
return wstr_dest;
}
iconv_t handle = (void*)-;
switch (code_page)
{
case CP_GBK:
handle = iconv_open("UCS-4", "GBK");
break;
case CP_UTF8:
handle = iconv_open("UCS-4", "UTF-8");
break;
default:
//不支持
break;
}
if (handle == (void*)-)
{
delete[] wcs;
return wstr_dest;
} size_t nsrc = str.size()*sizeof(char);
char* src = (char*)str.c_str();
wchar_t* tmp = wcs;
size_t ndst = OUT_LEN * sizeof(wchar_t);
//需多次转换,直到转换完毕
while (nsrc>)
{
memset(wcs, , OUT_LEN*sizeof(wchar_t));
tmp = wcs;
ndst = OUT_LEN * sizeof(wchar_t);
if (iconv(handle, (char**)&src, &nsrc, (char**)&tmp, &ndst) ==(size_t)- && errno != E2BIG)
{
wstr_dest.clear();
break;
}
wstr_dest += wstring(wcs, OUT_LEN - ndst/sizeof(wchar_t));
}
iconv_close(handle);
//释放临时缓冲区
delete[] wcs; #endif
return wstr_dest;
} /** @fn string ws2s(const wstring wstr, int code_page)
* @brief 从宽字符串转为多字节字符串
* @param wstr 源字符串
* @param code_page 要使用的代码页
* @return 成功返回多字节字符串,失败返回空字符串
*/
string ws2s(const wstring wstr, int code_page)
{
string str_dest;
if (wstr.size() == )
{
return str_dest;
}
char *mbs = NULL;
#ifdef _MSC_VER
int size = WideCharToMultiByte(code_page, , wstr.c_str(), -, NULL, , NULL, NULL);
mbs = new(std::nothrow) char[size];
if (NULL == mbs)
{
return str_dest;
}
if ( == WideCharToMultiByte(code_page, , wstr.c_str(), -, mbs, size, NULL, NULL))
{
str_dest.clear();
}
else
{
str_dest += mbs;
}
delete[] mbs;
#elif defined __linux
//申请临时缓冲区,用于保存转换后的字符串
mbs = new(std::nothrow)char[OUT_LEN];
if (NULL == mbs)
{
return str_dest;
}
iconv_t handle = (void*)-;
switch (code_page)
{
case CP_GBK:
handle = iconv_open("GBK", "UCS-4");
break;
case CP_UTF8:
handle = iconv_open("UTF-8", "UCS-4");
break;
default:
//不支持
break;
}
if (handle == (void*)-)
{
delete[] mbs;
return str_dest;
} size_t nsrc = wstr.size() * sizeof(wchar_t);
wchar_t* src = (wchar_t*)wstr.c_str();
char* tmp = NULL;
size_t ndst = OUT_LEN;
//需多次转换,直到转换完毕
while (nsrc>)
{
memset(mbs, , OUT_LEN);
tmp = mbs;
ndst = OUT_LEN;
if (iconv(handle, (char**)&src, &nsrc, (char**)&tmp, &ndst) ==(size_t)- && errno != E2BIG)
{
str_dest.clear();
break;
}
str_dest += string(mbs, OUT_LEN - ndst);
}
iconv_close(handle);
//释放临时缓冲区
delete[] mbs; #endif
return str_dest;
} /** @fn string utf82gbk(const string str_utf8)
* @brief 从UTF-8字符串转为GBK字符串
* @param str_utf8 源字符串
* @return 成功返回GBK字符串,失败返回空字符串
*/
string utf82gbk(const string str_utf8)
{
string str_gbk;
#ifdef _MSC_VER
wstring wstr = s2ws(str_utf8, CP_UTF8);
str_gbk = ws2s(wstr, CP_GBK);
#elif defined __linux
//申请临时缓冲区,用于保存转换后的字符串
char* gbk = new(std::nothrow)char[OUT_LEN];
if (NULL == gbk)
{
return str_gbk;
}
iconv_t handle = iconv_open("GBK", "UTF-8");
if (handle == (void*)-)
{
delete[] gbk;
return str_gbk;
}
size_t nsrc = str_utf8.size();
char* src = (char*)str_utf8.c_str();
char* tmp = NULL;
size_t ndst = OUT_LEN;
//需多次转换,直到转换完毕
while (nsrc > )
{
memset(gbk, , OUT_LEN);
tmp = gbk;
ndst = OUT_LEN;
if (iconv(handle, (char**)&src, &nsrc, (char**)&tmp, &ndst) ==(size_t)- && errno != E2BIG)
{
str_gbk.clear();
break;
}
str_gbk += string(gbk, OUT_LEN - ndst);
}
iconv_close(handle);
//释放临时缓冲区
delete[] gbk;
#endif
return str_gbk;
} /** @fn string gbk2utf8(const string str_gbk)
* @brief 从GBK字符串转为UTF-8字符串
* @param str_gbk 源字符串指针
* @return 成功返回UTF-8字符串,失败返回空字符串
*/
string gbk2utf8(const string str_gbk)
{
string str_utf8;
#ifdef _MSC_VER
wstring wstr = s2ws(str_gbk, CP_GBK);
str_utf8 = ws2s(wstr, CP_UTF8);
#elif defined __linux
//申请临时缓冲区,用于保存转换后的字符串
char* utf8 = new(std::nothrow)char[OUT_LEN];
if (NULL == utf8)
{
return str_utf8;
}
iconv_t handle = iconv_open("UTF-8", "GBK");
if (handle == (void*)-)
{
delete[] utf8;
return str_utf8;
}
size_t nsrc = str_gbk.size();
char* src = (char*)str_gbk.c_str();
char* tmp = NULL;
size_t ndst = OUT_LEN;
//需多次转换,直到转换完毕
while (nsrc > )
{
memset(utf8, , OUT_LEN);
tmp = utf8;
ndst = OUT_LEN;
if (iconv(handle, (char**)&src, &nsrc, (char**)&tmp, &ndst) ==(size_t)- && errno != E2BIG)
{
str_utf8.clear();
break;
}
str_utf8 += string(utf8, OUT_LEN - ndst);
}
iconv_close(handle);
//释放临时缓冲区
delete[] utf8;
#endif
return str_utf8;
} //wchar_t转成UTF-8
int Wchar2Utf8Convert( const wchar_t* a_szSrc, char* a_szDest, int a_nDestSize )
{
#if (defined _WIN32 || defined _WIN64)
return WideCharToMultiByte( CP_UTF8, , a_szSrc, -, a_szDest, a_nDestSize, NULL, NULL );
#elif defined(__linux__)
size_t result;
size_t srcSize = (wcslen(a_szSrc)+)*sizeof(wchar_t);
iconv_t env;
env = iconv_open("UTF-8","WCHAR_T");
if (env==(iconv_t)-)
{
//printf("iconv_open WCHAR_T->UTF8 error%s %d/n",strerror(errno),errno) ;
return ;
}
size_t buf_count = a_nDestSize;
result = iconv(env,(char**)&a_szSrc,(size_t*)&srcSize,(char**)&a_szDest,(size_t*)&buf_count);
if (result==(size_t)-)
{
//printf("iconv WCHAR_T->UTF8 error %d/n",errno) ;
return ;
}
iconv_close(env);
return (int)result;
#endif
} //UTF-8转成wchar_t
int Utf82WcharConvert( const char* a_szSrc, wchar_t* a_szDest, int a_nDestSize )
{
#if (defined _WIN32 || defined _WIN64)
return MultiByteToWideChar( CP_UTF8, , a_szSrc, -, a_szDest, a_nDestSize );
#elif defined(__linux__)
size_t result;
iconv_t env;
size_t size = strlen(a_szSrc)+ ;
env = iconv_open("WCHAR_T","UTF-8");
if (env==(iconv_t)-)
{
//printf("iconv_open UTF8->WCHAR_T error %d/n",errno) ;
return ;
}
size_t buf_count = a_nDestSize*sizeof(wchar_t);
result = iconv(env,(char**)&a_szSrc,(size_t*)&size,(char**)&a_szDest,(size_t*)&buf_count);
if (result==(size_t)-)
{
//printf("iconv UTF8->WCHAR_T error %d/n",errno) ;
return ;
}
iconv_close(env);
return (int)result; #endif
}
跨平台字符编码转换GBK、UTF8的更多相关文章
- 字符编码-UNICODE,GBK,UTF-8区别【转转】
字符编码介绍及不同编码区别 今天看到这篇关于字符编码的文章,抑制不住喜悦(总结的好详细)所以转到这里来.转自:祥龙之子http://www.cnblogs.com/cy163/archive/2007 ...
- SAP系统跨平台字符编码转换
SAP系统在进行了夸平台的迁移,可能会遇到操作系统层文件编码不同,导致SAP系统无法识别或者乱码的问题.例如SAP系统从AIX平台迁移到linux平台,SAP应用服务器的编码会发生变化,从4102变化 ...
- php字符编码转换之gb2312转为utf8(转)
在php中字符编码转换我们一般会用到iconv与mb_convert_encoding进行操作,但是mb_convert_encoding在转换性能上比iconv要差很多哦.string iconv ...
- 字符编码之间的相互转换 UTF8与GBK(转载)
转载自http://www.cnblogs.com/azraelly/archive/2012/06/21/2558360.html UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 ...
- Linux字符编码默认为UTF-8,如出现乱码可设置为GBK
Linux字符编码默认为UTF-8,如出现乱码可设置为GBK1.手动更改profile文件的命令: vi /etc/profile 也可以修改 /etc/sysconfig/i18n 文件,如 LAN ...
- iconv字符编码转换
转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...
- php 字符编码转换函数 iconv mb_convert_encoding比较
在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...
- 编码问题 php字符编码转换类
各种平台和软件打开显示的编码问题,需要使用不同的编码,根据我们不同的需求. php 字符编码转换类,支持ANSI.Unicode.Unicode big endian.UTF-8.UTF-8+Bom ...
- Python—字符编码转换、函数基本操作
字符编码转换 函数 #声明文件编码,格式如下: #-*- coding:utf-8 -*- 注意此处只是声明了文件编码格式,python的默认编码还是unicode 字符编码转换: import sy ...
随机推荐
- JS 对java返回的json格式的数据处理
var dataObj=eval("("+res+")"); alert(dataObj.billBuy) //res是如下的数据 {"billBuy ...
- Navicat: Can't create a procedure from within another stored routine
测试调用mysql的存储过程,于是用Navicat写,结果报这个错误,源代码如下: CREATE PROCEDUREQueryDate() BEGIN SELECTCURDATE(); E ...
- Vue解决安卓4.4不兼容的问题
1.npm安装 npm install babel-polyfillnpm install es6-promise package.json中会出现 "babel-polyfill" ...
- linux文件名乱码时删除或改名的方式(转载)
转自:http://www.linuxsa.cn/when-linux-file-name-topsy-turvy-deleted-or-renamed.html linux文件名乱码时删除或改名的方 ...
- Swift4 函数, 元组, 运算符
创建: 2018/02/19 完成: 2018/02/19 更新: 2018/02/25 修改标题 [Swift4 函数] -> [Swift4 函数, 元组, 运算符] 更新 :2018/03 ...
- PhpStorm Xdebug远程调试环境搭建原理分析及问题排查
2017年05月26日 经验心得 目录 一. 环境介绍 二. 远程环境配置 2.2 Xdebug安装 2.3 配置 三. 本地phpstorm配置 3.1 下载远程代码 3.2 添加php解释器 ...
- head first python /chapter7 web(python 3 转 python 2.7)
前言 书中使用的是python3,我这里使用的是python2.7 Web 的目录树 webapp/ ├── cgi-bin │ ├── athletelist.py │ ├── athletemod ...
- _bzoj1798 [Ahoi2009]Seq 维护序列seq【线段树 lazy tag】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 注意,应保证当前节点维护的值是正确的,lazy tag只是一个下传标记,在下传时应即时 ...
- ACM二分查找模板
int main(){ == key int m; while ( l <= r ) { m = ( l + r ) >> 1; if ( x[m] == key ) return ...
- 题解报告:hdu 2057 A + B Again
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2057 问题描述 我们的HDOJ必须有许多A + B问题,现在又有新的问题出现. 给你两个十六进制整数, ...