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 ...
随机推荐
- python 流程控制(for循环语句)
1,for循环基本语法 2, for循环常用序列 3,for循环 else使用方法 1,for循环基本语法 for iterating_var in sequence: statements(s) 2 ...
- C语言short int
因为C语言中short int占2个字节,有16个二进制位,共可表示2^16种状态.因为它用来表示有符号数,而0也要占用一个状态.所以,16位的原码可以表示的数是-32767~+32767,它的0可以 ...
- 【转】HTTP学习---Web 缓存
[原文]https://www.toutiao.com/i6592743068623962632/ 1. 前端缓存概述 前端缓存主要是分为HTTP缓存和浏览器缓存.其中HTTP缓存是在HTTP请求传输 ...
- 正确计算linux系统内存使用率
参考:https://blog.gesha.net/archives/406/ 图中的例子很典型,就是:多数的linux系统在free命令后会发现free(剩余)的内存很少,而自己又没有开过多的程序或 ...
- 未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,
未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft ...
- IIS 注册Asp.net 4.0
32位的Windows:---------------------------------------------------------------------------1. 运行->cmd ...
- 《JavaScript高级程序设计》读书笔记--ECMAScript中所有函数的参数都是按值传递的
ECMAScript中所有函数的参数都是按值传递的.也就是说把函数外部的值复制给函数内部的参数(内部参数的值的修改不影响实参的值). 基本类型变量的复制: 基本类型变量的复制,仅仅是值复制,num1和 ...
- BZOJ2893:征服王(费用流)
Description 虽然春希将信息传递给了雪菜,但是雪菜却好像完全不认得春希了.心急如焚的春希打开了第二世代机能,对雪菜的脑内芯片进行了直连-hack. 进入到雪菜内部的春希发现(这什么玩意..) ...
- Thinkphp5.0分页和跳页
后台查询商品或者会员量需要用到分页展示列表,当页数比较多的时候为了体高用户体验度,需要添加一个跳页也就是手动输入页码数进行快速跳转指定页面.由于手动编写分页比较麻烦,又想使用TP5自带的分页,但是TP ...
- opengl渲染管线梳理
opengl渲染管线梳理 http://www.cnblogs.com/zhanglitong/p/3238989.html 坐标系变换和矩阵 http://www.cppblog.com/guoji ...