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互转的更多相关文章

  1. 【NodeJs】Ctrl+C在Linux平台和Windows平台下的TCP连接中的不同表现

    Linux平台:CentOS release 6.5 (Final) Windows平台:Windows 7 旗舰版 服务器端代码如下: var net = require('net'); var s ...

  2. Mac平台与Windows平台下AndroidStudio增量升级

    Android Studio增量升级什么情况下使用最合适呢? 比如现在的as版本是2.2版本,而你的as版本2.0版本,这个时候点Check For Updates就没有反应了,因为你已经2个有版本没 ...

  3. windows平台下nginx+PHP环境安装

    因为日常工作在windows下,为方便在window是下进行PHP开发,需要在windows平台下搭建PHP开发环境,web服务器选择nginx,不过windows版本的nginx性能要比Linux/ ...

  4. 利用zabbix监控ogg进程(Windows平台下)

    本文给大家介绍如何监控windows平台下的ogg程序.(注:所有操作都在administrator用户下面进行操作) 监控linux平台下的ogg程序请看:https://www.cnblogs.c ...

  5. [转]Windows平台下Makefile学习笔记

    Windows平台下Makefile学习笔记(一) 作者:朱金灿 来源:http://blog.csdn.net/clever101 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译 ...

  6. windows平台下VLC2.0.5编译

    windows平台下VLC2.0.5编译说明 时隔一年多,又要搞流媒体了,不过这次是要做流媒体服务器. 暂时决定使用vlc+ffmpeg+live555,虽然听有些前辈说这个组合的性能较差,只能作为学 ...

  7. 【转】Windows平台下Git服务器搭建

    Windows平台下Git服务器搭建 Posted on 2015-05-18 21:29 阿祥当码农 阅读(7637) 评论(0) 编辑 收藏 该文章转自:http://www.codeceo.co ...

  8. windows平台下的oracle ORA-01031的解决方法

    今天下午遇到一个很怪异的问题,在windows平台下sqlplus  / as sysdba登陆数据库,提示权限不足, 当时就纳闷了,sys用户登陆数据库还能权限不足,问题出现了,就开始寻找解决方法呗 ...

  9. Windows平台下MySQL常用操作与命令

    Windows平台下MySQL常用操作与命令 Windows平台下MySQL常用操作与命令,学习mysql的朋友可以参考下. 1.导出整个数据库 mysqldump -u 用户名 -p --defau ...

随机推荐

  1. LeetCode题解之Rotate String

    1.题目描述 2.问题分析 直接旋转字符串A,然后做比较即可. 3.代码 bool rotateString(string A, string B) { if( A.size() != B.size( ...

  2. 将目录结构输出为json格式(zTree)

    # -*- coding: UTF-8 -*- import json,os path = 'E:\\BACKUP' #返回空目录 def path_to_dict(path): d = {'name ...

  3. python 多进程 Event的使用

    Event事件  多进程的使用 通俗点儿讲  就是 1.  Event().wait()    插入在进程中插入一个标记(flag)  默认为 false  然后flag为false时  程序会停止运 ...

  4. 【转】Spring学习---为什么要用spring,springMVC

    [原文]https://www.toutiao.com/i6593182323095634445/ 首先,软件里有很多优秀的框架,有一种类型的框架,它的特点是建立在一个现有技术的基础上,提供和现有技术 ...

  5. 【转】Java学习---深入理解线程池

    [原文]https://www.toutiao.com/i6566022142666736131/ 我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很 ...

  6. ORACLE 查看并修改最大连接数

    第一步,在cmd命令行,输入sqlplus,打开登录窗口,如下: 第二步,根据提示输入用户名与密码 请输入用户名:sys as sysdba 输入口令:******** 第三步,查看processes ...

  7. 利用VBA宏解除Excel保护

    复制以下代码并录制宏,运行一次即可. Option Explicit Public Sub AllInternalPasswords() ' Breaks worksheet and workbook ...

  8. elasticsearch报错之 memory locking requested for elasticsearch process but memory is not locked

    安装elasticsearch报错如下: [2019-01-14T03:57:16,453][ERROR][o.e.b.Bootstrap ] [ip-172-31-30-62.ec2.interna ...

  9. System.IO.Path文件路径类

    Path类的静态属性和方法,此类操作不影响物料文件. 属性 char a = System.IO.Path.VolumeSeparatorChar;//: char b = System.IO.Pat ...

  10. IOS和安卓不同浏览器常见bug

    一.IOS自带safari浏览器 1.safari不支持fixed.input输入框 iOS下的 Fixed + Input 调用键盘的时候fixed无效问题 拖动页面时 header 和 foote ...