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. ssh免秘钥配置

    1.要求:两台相同的系统,这里设置server1控制server2的服务器 2.先在两台服务器上都安装好ssh 输入命令下载: yum install -y openssh-clients opens ...

  2. XML与DataSet的相互转换

    转:https://www.cnblogs.com/kunEssay/p/6168824.html XML与DataSet的相互转换的类 一.XML与DataSet的相互转换的类 using Syst ...

  3. c#WebApi使用form表单提交excel,实现批量写入数据库

    思路:用户点击下载模板按钮,获取到excel模板,然后向里面填写数据保存.from表单提交的时候选择保存好的excel,实现数据的批量导入过程 先把模板放在服务器的项目目录下面:如 模板我一般放在:F ...

  4. Oracle EBS AR 贷项通知单核销取值

    SELECT cm.trx_number ,fnd_flex_ext.get_segs('SQLGL', 'GL#', gcc.chart_of_accounts_id, ad.code_combin ...

  5. 忘了mysql密码咋么办?

    一.已知mysql数据库root的密码,修改root的方法如下: 1. mysqladmin 命令直接修改: ~]#mysqladmin -u root -p password "newpa ...

  6. MySQL索引设计不可忽视的知识点

    本文主要讨论MySQL索引的部分知识.将会从MySQL索引基础.索引优化实战和数据库索引背后的数据结构三部分相关内容,下面一一展开. 一.MySQL——索引基础 首先,我们将从索引基础开始介绍一下什么 ...

  7. tcp的半连接与完全连接队列

    队列及参数 https://segmentfault.com/a/1190000008224853 server端的半连接队列(syn队列) 在三次握手协议中,服务器维护一个半连接队列,该队列为每个客 ...

  8. OpenGL_Qt学习笔记之_03(平面图形的着色和旋转)(转)

    http://www.cnblogs.com/tornadomeet/archive/2012/08/23/2653305.html 在这一节中主要简单介绍下怎样给平面几何着色,以及怎样让绘制出来的几 ...

  9. Spring Boot 验证表单

    在实际工作中,得到数据后的第一步就是验证数据的正确性,如果存在录入上的问题,一般会通过注解校验,发现错误后返回给用户,但是对于逻辑上的错误,很难使用注解方式进行验证了,这个使用可以使用Spring所提 ...

  10. ubuntu环境下docker的安装与操作

    只要按照本文的步骤一步步的走,就能正确的安装docker并使用,ubuntu需要联网 1. 在Ubuntu中安装Docker 更新ubuntu的apt源索引 sudo apt-get update 安 ...