characterCoder.h
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
*字符转码的头文件
*/
#ifndef _CHARACTER_CODER_H
#define _CHARACTER_CODER_H
#include "iconv.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int code_convert(char *from_charset,char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
iconv_t cd;
int ret = 0;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd == (iconv_t)-1)
{
// perror("-----------------FAILED: iconv_open----------\n");
return -1;
}
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen) == -1)
ret = -1;
iconv_close(cd);
return ret;
}
int utf2gbk(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
// return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
return code_convert("utf-8","gbk",inbuf,inlen,outbuf,outlen);
}
int gbk2utf(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
// return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
return code_convert("gbk","utf-8",inbuf,inlen,outbuf,outlen);
}
#endif