利用iconv()函数可以把GB2312汉字转为Unicode,可怎么能使输入为GB2312 机内码呢。
源码如下:
#include "iconv.h"
#include <string.h>
#include <stdio.h>
#define OUTLEN 255
/*代码转换:从一种编码转为另一种编码*/
int code_convert(char *from_charset,char *to_charset,const char *inbuf,unsigned int inlen,
unsigned char *outbuf,unsigned int outlen)
{
iconv_t cd;
int rc;
const char **pin = &inbuf;
unsigned char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
/*GB2312码转为utf-8码*/
int g2u(const char *inbuf,unsigned int inlen,unsigned char *outbuf,unsigned int outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}
void main()
{
[color=#FF0000]const char *in_gb2312 = "啊";
//这怎么修改为‘啊’的区位码0xB0A1呢.[/color]
unsigned char out[OUTLEN];
int rc;
unsigned int length_gb2312;
//gb2312码转为utf8码
length_gb2312= strlen(in_gb2312);
rc = g2u(in_gb2312,length_gb2312,out,OUTLEN);
//utf8转unicode码
unsigned short unicode;
unicode = out[0];
if (unicode >= 0xF0) {
unicode = (unsigned short) (out[0] & 0x07) < < 18;
unicode |= (unsigned short) (out[1] & 0x3F) < < 12;
unicode |= (unsigned short) (out[2] & 0x3F) < < 6;
unicode |= (unsigned short) (out[3] & 0x3F);
} else if (unicode >= 0xE0) {
unicode = (unsigned short) (out[0] & 0x0F) < < 12;
unicode |= (unsigned short) (out[1] & 0x3F) < < 6;
unicode |= (unsigned short) (out[2] & 0x3F);
} else if (unicode >= 0xC0) {
unicode = (unsigned short) (out[0] & 0x1F) < < 6;
unicode |= (unsigned short) (out[1] & 0x3F);
}
printf("gb2312-->utf8 out=%x /n",out);
printf("unicode=%x /n",unicode);
}
-------------
输出为:
gb2312-->utf8 out=12fe7c
unicode=554a
Press any key to continue
如给出正确答案,再加50分。
iconv
最新推荐文章于 2022-01-13 18:58:01 发布