首页 技术 正文
技术 2022年11月6日
0 收藏 303 点赞 691 浏览 2143 个字

参考文章:http://jimmee.iteye.com/blog/2174693

关于windows上编译libiconv的库,请参见:http://www.cnblogs.com/tangxin-blog/p/5608751.html

 #include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "iconv.h" #define MAX_BUF_SIZE 1024 int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen,
char *outbuf, size_t outlen) {
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf; cd = iconv_open(to_charset, from_charset);
if (cd == )
return -;
memset(outbuf, , outlen);
if (iconv(cd, pin, &inlen, pout, &outlen) == -)
return -;
iconv_close(cd);
*pout = '\0'; return ;
} int utf8_to_gbk(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("utf-8", "gbk", inbuf, inlen, outbuf, outlen);
} int gbk_to_utf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("gbk", "utf-8", inbuf, inlen, outbuf, outlen);
} void read_file(char buf[], const int32_t max_buf_size, const char *file_name)
{
FILE * pFile;
long lSize;
size_t result;
fopen_s(&pFile, file_name, "rb");
if (pFile == NULL) { fputs("File error\n", stderr); exit(); }
// obtain file size:
fseek(pFile, , SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
if (lSize >= max_buf_size){ fputs("file too large\n", stderr); exit(); }
result = fread(buf, , lSize, pFile);
if (result != lSize) { fputs("Reading error\n", stderr); exit(); }
fclose(pFile);
} //将gbk编码的str分隔成一个一个的字符,并判断是否是汉字,并输出编码,包括简体和繁体
void GetToken(const char *str)
{
int32_t i = ;
int32_t len = strlen(str);
short high, low;
uint32_t code;
char cstr[];
for (; i < len; ++i)
{
if (str[i] >= || i == len - )
{
printf("%c >> no\n", str[i]); //ASCII字符
}
else
{
// 计算编码
high = (short)str[i] + ;
low = (short)str[i + ] + ;
code = high * + low; //获取字符
cstr[] = str[i];
cstr[] = str[i + ];
cstr[] = ;
i++; printf("%s >> 0x%x", cstr, code);
if ((code >= 0xB0A1 && code <= 0xF7FE) || (code >= 0x8140 && code <= 0xA0FE) || (code >= 0xAA40 && code <= 0xFEA0))
{
printf(" yes\n");
}
else
{
printf(" no\n");
}
}
}
} int main(int argc, char *argv[])
{
char in_buf[MAX_BUF_SIZE] = { }, out_buf[MAX_BUF_SIZE] = { };
read_file(in_buf, MAX_BUF_SIZE, "chinese_gbk.txt");
printf("%s\n", in_buf);
GetToken(in_buf);
read_file(in_buf, MAX_BUF_SIZE, "chinese_utf8.txt");
printf("%s\n", in_buf);
GetToken(in_buf);
utf8_to_gbk(in_buf, strlen(in_buf), out_buf, MAX_BUF_SIZE);
printf("%s\n", out_buf);
GetToken(out_buf);
getchar();
return ;
}

完整工程demo:http://download.csdn.net/detail/tangxin19930330/9557218

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300