Linux 平台和 Windows平台下 Unicode与UTF-8互转
Windows:
unsigned char * make_utf8_string(const wchar_t *unicode)
{
int size = , index = , out_index = ;
unsigned char *out;
unsigned short c; /* first calculate the size of the target string */
c = unicode[index++];
while(c)
{
if(c < 0x0080)
{
size += ;
}
else if(c < 0x0800)
{
size += ;
}
else
{
size += ;
} c = unicode[index++];
} out = (unsigned char*)malloc(size + );
if (out == NULL)
return NULL; index = ; c = unicode[index++];
while(c)
{
if(c < 0x080)
{
out[out_index++] = (unsigned char)c;
}
else if(c < 0x800)
{
out[out_index++] = 0xc0 | (c >> );
out[out_index++] = 0x80 | (c & 0x3f);
}
else
{
out[out_index++] = 0xe0 | (c >> );
out[out_index++] = 0x80 | ((c >> ) & 0x3f);
out[out_index++] = 0x80 | (c & 0x3f);
}
c = unicode[index++];
} out[out_index] = 0x00; return out;
} wchar_t * make_unicode_string(const unsigned char *utf8)
{
int size = , index = , out_index = ;
wchar_t *out;
unsigned char c; /* first calculate the size of the target string */
c = utf8[index++];
while(c)
{
if((c & 0x80) == )
{
index += ;
}
else if((c & 0xe0) == 0xe0)
{
index += ;
}
else
{
index += ;
} size += ;
c = utf8[index++];
} out = (wchar_t*)malloc((size + ) * sizeof(wchar_t));
if (out == NULL)
return NULL; index = ; c = utf8[index++];
while(c)
{
if((c & 0x80) == )
{
out[out_index++] = c;
}
else if((c & 0xe0) == 0xe0)
{
out[out_index] = (c & 0x1F) << ;
c = utf8[index++];
out[out_index] |= (c & 0x3F) << ;
c = utf8[index++];
out[out_index++] |= (c & 0x3F);
}
else
{
out[out_index] = (c & 0x3F) << ;
c = utf8[index++];
out[out_index++] |= (c & 0x3F);
} c = utf8[index++];
} out[out_index] = ; return out;
} int StrUtil::utf8_encode(const char *from, char **to)
{
wchar_t *unicode;
int wchars, err; wchars = ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from,
strlen(from), NULL, ); if (wchars == )
{
fprintf(stderr, "Unicode translation error %d\n", GetLastError());
return -;
} unicode = (wchar_t*)calloc(wchars + , sizeof(unsigned short));
if(unicode == NULL)
{
fprintf(stderr, "Out of memory processing string to UTF8\n");
return -;
} err = ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from,
strlen(from), unicode, wchars);
if(err != wchars)
{
free(unicode);
fprintf(stderr, "Unicode encode error %d\n", GetLastError());
return -;
} /* On NT-based windows systems, we could use WideCharToMultiByte(), but
* MS doesn't actually have a consistent API across win32.
*/
*to = (char *)make_utf8_string(unicode); free(unicode);
return ;
} int StrUtil::utf8_decode(const char *from, char **to)
{
wchar_t *unicode;
int chars, err; /* On NT-based windows systems, we could use MultiByteToWideChar(CP_UTF8), but
* MS doesn't actually have a consistent API across win32.
*/
unicode = make_unicode_string((unsigned char*)from);
if(unicode == NULL)
{
fprintf(stderr, "Out of memory processing string from UTF8 to UNICODE16\n");
return -;
} chars = ::WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
-, NULL, , NULL, NULL); if(chars == )
{
fprintf(stderr, "Unicode translation error %d\n", GetLastError());
free(unicode);
return -;
} *to = (char *)calloc(chars + , sizeof(unsigned char));
if(*to == NULL)
{
fprintf(stderr, "Out of memory processing string to local charset\n");
free(unicode);
return -;
} err = ::WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
-, *to, chars, NULL, NULL);
if(err != chars)
{
fprintf(stderr, "Unicode decode error %d\n", GetLastError());
free(unicode);
free(*to);
*to = NULL;
return -;
} free(unicode);
return ;
}
Linux 平台:
unsigned char * make_utf8_string(const wchar_t *unicode)
{
int size = , index = , out_index = ;
unsigned char *out;
unsigned short c; /* first calculate the size of the target string */
c = unicode[index++];
while(c)
{
if(c < 0x0080)
{
size += ;
}
else if(c < 0x0800)
{
size += ;
}
else
{
size += ;
} c = unicode[index++];
} out = (unsigned char*)malloc(size + );
if (out == NULL)
return NULL; index = ; c = unicode[index++];
while(c)
{
if(c < 0x080)
{
out[out_index++] = (unsigned char)c;
}
else if(c < 0x800)
{
out[out_index++] = 0xc0 | (c >> );
out[out_index++] = 0x80 | (c & 0x3f);
}
else
{
out[out_index++] = 0xe0 | (c >> );
out[out_index++] = 0x80 | ((c >> ) & 0x3f);
out[out_index++] = 0x80 | (c & 0x3f);
}
c = unicode[index++];
} out[out_index] = 0x00; return out;
} wchar_t * make_unicode_string(const unsigned char *utf8)
{
int size = , index = , out_index = ;
wchar_t *out;
unsigned char c; /* first calculate the size of the target string */
c = utf8[index++];
while(c)
{
if((c & 0x80) == )
{
index += ;
}
else if((c & 0xe0) == 0xe0)
{
index += ;
}
else
{
index += ;
} size += ;
c = utf8[index++];
} out = (wchar_t*)malloc((size + ) * sizeof(wchar_t));
if (out == NULL)
return NULL; index = ; c = utf8[index++];
while(c)
{
if((c & 0x80) == )
{
out[out_index++] = c;
}
else if((c & 0xe0) == 0xe0)
{
out[out_index] = (c & 0x1F) << ;
c = utf8[index++];
out[out_index] |= (c & 0x3F) << ;
c = utf8[index++];
out[out_index++] |= (c & 0x3F);
}
else
{
out[out_index] = (c & 0x3F) << ;
c = utf8[index++];
out[out_index++] |= (c & 0x3F);
} c = utf8[index++];
} out[out_index] = ; return out;
}
int utf8_encode(const char *from, char **to)
{
wchar_t *unicode = NULL;
int wchars, err; setlocale(LC_ALL,"");
wchars = mbstowcs(unicode, from, )+; unicode = new wchar_t[wchars]; err = mbstowcs(unicode, from, wchars);
if(err < )
{
delete unicode;
fprintf(stderr, "Unicode encode error \n");
return -;
} setlocale(LC_ALL,"C"); *to = (char *)make_utf8_string(unicode); delete unicode; return ;
} int utf8_decode(const char *from, char **to)
{
wchar_t *unicode = NULL;
int chars, err; // setlocale(LC_ALL,"zh_CN.GB18030"); unicode = make_unicode_string((unsigned char*)from); setlocale(LC_ALL,"");
chars = wcstombs(*to,unicode, )* + ; *to = new char[chars];
memset(*to, , chars); //setlocale(LC_ALL,"");
err = wcstombs(*to, unicode, chars);
setlocale(LC_ALL,"C");
delete unicode;
if(err < )
{
fprintf(stderr, "Unicode decode error \n");
delete *to;
*to = NULL;
return -;
} return ;
}
Linux 平台和 Windows平台下 Unicode与UTF-8互转的更多相关文章
- 【NodeJs】Ctrl+C在Linux平台和Windows平台下的TCP连接中的不同表现
Linux平台:CentOS release 6.5 (Final) Windows平台:Windows 7 旗舰版 服务器端代码如下: var net = require('net'); var s ...
- Mac平台与Windows平台下AndroidStudio增量升级
Android Studio增量升级什么情况下使用最合适呢? 比如现在的as版本是2.2版本,而你的as版本2.0版本,这个时候点Check For Updates就没有反应了,因为你已经2个有版本没 ...
- windows平台下nginx+PHP环境安装
因为日常工作在windows下,为方便在window是下进行PHP开发,需要在windows平台下搭建PHP开发环境,web服务器选择nginx,不过windows版本的nginx性能要比Linux/ ...
- 利用zabbix监控ogg进程(Windows平台下)
本文给大家介绍如何监控windows平台下的ogg程序.(注:所有操作都在administrator用户下面进行操作) 监控linux平台下的ogg程序请看:https://www.cnblogs.c ...
- [转]Windows平台下Makefile学习笔记
Windows平台下Makefile学习笔记(一) 作者:朱金灿 来源:http://blog.csdn.net/clever101 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译 ...
- windows平台下VLC2.0.5编译
windows平台下VLC2.0.5编译说明 时隔一年多,又要搞流媒体了,不过这次是要做流媒体服务器. 暂时决定使用vlc+ffmpeg+live555,虽然听有些前辈说这个组合的性能较差,只能作为学 ...
- 【转】Windows平台下Git服务器搭建
Windows平台下Git服务器搭建 Posted on 2015-05-18 21:29 阿祥当码农 阅读(7637) 评论(0) 编辑 收藏 该文章转自:http://www.codeceo.co ...
- windows平台下的oracle ORA-01031的解决方法
今天下午遇到一个很怪异的问题,在windows平台下sqlplus / as sysdba登陆数据库,提示权限不足, 当时就纳闷了,sys用户登陆数据库还能权限不足,问题出现了,就开始寻找解决方法呗 ...
- Windows平台下MySQL常用操作与命令
Windows平台下MySQL常用操作与命令 Windows平台下MySQL常用操作与命令,学习mysql的朋友可以参考下. 1.导出整个数据库 mysqldump -u 用户名 -p --defau ...
随机推荐
- Mongodb集群——master/slave
集群的配置 (本测试放于同一台机器进行配置,所以IP地址一样,如果是在不同的服务器上更换IP便可以) 1.目录结构 拷贝两份mongodb到/home/scotte.ye/mongo1 ...
- 什么是 Azure 中的虚拟机规模集?
虚拟机规模集是一种 Azure 计算资源,可用于部署和管理一组相同的 VM. 由于所有 VM 的配置都相同,因此无需对 VM 进行任何预先配置. 这样就可以更方便地构建面向大型计算.大数据.容器化工作 ...
- 推荐linux下的数据库开发工具DBeaver 开源免费
linux下不错的数据库管理工具 DBeaver 操作比较友好,基于eclipse.使用jdbc链接,链接数据库非常全(oracle mysql mssql sqlite 常见的统统支持).而且可以 ...
- 向kindle传送文件
自从有了kindle以后,只要不是pdf的电子书,基本上都用kindle来看了,毕竟水墨屏还是很护眼的,但是这就带来了向kindle传输文件的问题 绝大部分的电子书用邮件推送就可以解决了,但是还有一些 ...
- MongoDB基础之 安装
本篇来介绍MongoDB的安装.安装版本是3.4.18 如果安装老版本的mongodb,你会发现安装比较顺利,但是新版本的mongo安装稍微麻烦一点 ,主要是多了安全机制认证等步骤. 1.下载mong ...
- 3.2Python的循环结构语句:
返回总目录 目录: 1.while循环 2.for循环 3.循环保留字:break与continue 循环总览: (一)while循环: (1)单个while循环: while 条件: 循环体 ...
- css多行省略-webkit-box-orient打包编译后失效原因
原css代码: .dialog-for-plan .dialog-table .type>p { display: -webkit-box; -webkit-box-orient: vertic ...
- python第三十九课——面向对象(二)之初始化属性
设计Car类,初始化属性speed,提供一个run函数 import time class Car: def __init__(self,speed): self.speed=speed #将Road ...
- Mysql表创建外键报错
数据库表A: CREATE TABLE task_desc_tab ( id INT(11) PRIMARY KEY NOT NULL COMMENT '自增主键' AUTO_INCREMENT, t ...
- centos7 安装svn, 同时支持 svn 和 http访问
1. 安装 svn [root@svn conf]#yum install subversion [root@svn conf]# svn --version svn, version 1.7.14 ...