参考文章: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