Ai
1 Star 0 Fork 0

zdevt/cpptest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
testcode.cpp 2.68 KB
一键复制 编辑 原始数据 按行查看 历史
zdevt 提交于 2023-08-13 12:48 +08:00 . up
/*
* =====================================================================================
*
* Filename: testcode.cpp
*
* Description:
*
* Version: 1.0
* Created: 2017年10月10日 13时37分58秒
* Last Modified: 2017年10月10日 13时37分58秒
* Revision: none
* Compiler: gcc
*
* Author: zt (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <iconv.h>
int unicode_to_utf8 ( char* inbuf, size_t* inlen, char* outbuf, size_t* outlen )
{
char* encTo = "UTF-8//IGNORE";
char* encFrom = "UNICODE";
iconv_t cd = iconv_open ( encTo, encFrom );
if ( cd == ( iconv_t ) - 1 )
perror ( "iconv_open" );
char* tmpin = inbuf;
char* tmpout = outbuf;
size_t insize = *inlen;
size_t outsize = *outlen;
/* 进行转换
*@param cd iconv_open()产生的句柄
*@param srcstart 需要转换的字符串
*@param inlen 存放还有多少字符没有转换
*@param tempoutbuf 存放转换后的字符串
*@param outlen 存放转换后,tempoutbuf剩余的空间
*
* */
size_t ret = iconv ( cd, &tmpin, inlen, &tmpout, outlen );
if ( ret == -1 )
perror ( "iconv" );
iconv_close ( cd );
return outsize - *outlen;
}
size_t utf8_to_unicode ( char* inbuf, size_t* inlen, char* outbuf, size_t* outlen )
{
char* encTo = "UNICODE//IGNORE";
char* encFrom = "UTF-8";
iconv_t cd = iconv_open ( encTo, encFrom );
if ( cd == ( iconv_t ) - 1 )
perror ( "iconv_open" );
int ousize = *outlen;
size_t ret = iconv ( cd, &inbuf, inlen, &outbuf, outlen );
if ( ret == -1 )
perror ( "iconv" );
iconv_close ( cd );
return ousize - *outlen;
}
void printBuffer ( char* buff, int size )
{
for ( int i = 0; i < size; ++i )
{
if ( i % 0x10 == 0 && i != 0 )
printf ( "\n" );
printf ( "%02x ", ( unsigned char ) buff[i] );
}
printf ( "\n" );
}
int main ()
{
char text[1024] = "测试";
size_t inlen = strlen ( text );
char outbuf[1024] = {};
size_t outlen = 1024;
printBuffer ( text, inlen );
int outsize = utf8_to_unicode ( text, &inlen, outbuf, &outlen );
printBuffer ( outbuf, outsize );
char out2[1024] = { 0 };
size_t outsize2 = 1024;
size_t insize = outsize;
int len2 = unicode_to_utf8 ( outbuf, &insize, out2, &outsize2 );
printBuffer ( out2, len2 );
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zdevt/cpptest.git
git@gitee.com:zdevt/cpptest.git
zdevt
cpptest
cpptest
master

搜索帮助