(c语言)gb2312和utf8转换

(c语言)unicode和utf8转换

unicode和utf8转换规则



unicode与gb2312有着转换表


所以,只需要unicode和utf8之间进行转换即可


一、所以gb2312转utf8

void Gb2312ToUtf8(const char* input_file, const char *output_file)
{
printf("gb2312->utf8: \n");
//请在此处添加代码完成gb2312到utf8的转换
int byteCount=0;
int i=0;
int j=0;
u16 gbKey=0;
u16 unicodeKey=0;
long len;
FILE *fpIn=fopen(input_file,"rb");
if(fpIn==NULL){
printf("Unable to open the input file!\n");
        return;
}
else{
fseek( fpIn, 0L, SEEK_END );
        len = ftell( fpIn );
        printf( "intput file size: %ldB\n", len ); 
}
FILE* fpOut=fopen(output_file,"wb");
if(fpOut==NULL)
    {
        printf("Unable to open the output file!\n");
        return;
    }

u8 *gb,*temp;
gb=new u8[len*2];
temp=new u8[len*2];
fseek(fpIn,0L,SEEK_SET);
fread(gb,sizeof(u8),len,fpIn);
int count=0;
while(i<len){


memcpy(&gbKey,(gb+i),2);
gbKey=(gbKey >> 8) | (gbKey << 8);
unicodeKey=SearchCodeTable_GB2312(gbKey);
byteCount=0;


//unicodeKey->utf-8
if(unicodeKey==0){
printf("fail:table can not find the key: 0x%x \n",gbKey);
count++;
temp[j]=gb[i];
j++;
}
else {
if(unicodeKey<=0x0000007F){
temp[j]=unicodeKey&0x7F;
byteCount=1;
continue;
}
else if(unicodeKey>=0x00000080&&unicodeKey<0x000007FF){
temp[j+1]=(unicodeKey&0x3F)|0x80;
temp[j]=((unicodeKey>>6)&0x1F)|0xC0;
byteCount=2;
continue;
}
else if(unicodeKey>=0x00000800&&unicodeKey<=0x0000FFFF){
temp[j+2]=(unicodeKey&0x3F)|0x80;
temp[j+1]=((unicodeKey>>6)&0x3F)|0x80;
temp[j]=((unicodeKey>>12)&0x0F)|0xE0;
byteCount=3;
i++;
continue;
}
else if(unicodeKey>=0x00010000&&unicodeKey<=0x0010FFFF){
temp[j+3]=(unicodeKey&0x3F)|0x80;
temp[j+2]=((unicodeKey>>6)&0x3F)|0x80;
temp[j+1]=((unicodeKey>>12)&0x3F)|0x80;
temp[j]=((unicodeKey>>18)&0xF7);
byteCount=4;
continue;
}
else if(unicodeKey>=0x00200000&&unicodeKey<=0x03FFFFFF){
temp[j+4]=(unicodeKey&0x3F)|0x80;
temp[j+3]=((unicodeKey>>6)&0x3F)|0x80;
temp[j+2]=((unicodeKey>>12)&0x3F)|0x80;
temp[j+1]=((unicodeKey>>18)&0x3F)|0x80;
temp[j]=((unicodeKey>>24)&0xF7);
byteCount=5;
continue;
}
else if(unicodeKey>=0x04000000&&unicodeKey<=0x7FFFFFFF){
temp[j+5]=(unicodeKey&0x3F)|0x80;
temp[j+4]=((unicodeKey>>6)&0x3F)|0x80;
temp[j+3]=((unicodeKey>>12)&0x3F)|0x80;
temp[j+2]=((unicodeKey>>18)&0x3F)|0x80;
temp[j+1]=((unicodeKey>>24)&0x3F)|0x80;
temp[j]=((unicodeKey>>30)&0xF7);
byteCount=6;
continue;
}
else{
printf("out of unicodeKey ! \n");
continue;
}
}

j+=byteCount;
i+=1;


}
printf("There are %d  wrong!",count);
fwrite(temp, sizeof(u8),j, fpOut);
delete []gb;
delete []temp;
fclose(fpIn);
fclose(fpOut);


}


二、utf8转gb2312

void Utf8ToGb2312(const char* input_file, const char *output_file)
{
printf("utf8->unicode: \n");


int byteCount = 0;
int i = 0;
int j = 0;
u16 unicodeKey = 0;
u16 gbKey = 0;


long len;
FILE* fpIn=fopen(input_file,"rb");
if(fpIn==NULL)
    { 
        printf("Unabile to open the input file!\n");
        return;
    }
else
    { 
        // 将指针定位到文件末尾
        fseek( fpIn, 0L, SEEK_END );
        len = ftell( fpIn );
        printf( "intput file size: %ldB\n", len ); 
    }
FILE* fpOut=fopen(output_file,"wb");
if(fpOut==NULL)
    {
        printf("Unabile to open the output file!\n");
        return;
    }


u8 *utf8,*temp;
utf8=new u8[len];
temp=new u8[len];
    fseek( fpIn, 0L, SEEK_SET );
fread(utf8, sizeof(u8),len,fpIn);


i=3;
while (i < len)
{   
        switch(GetUtf8ByteNumForWord((u8)utf8[i]))
        {
case 0:
temp[j] = utf8[i];
byteCount = 1;
break;

case 2:
temp[j] = utf8[i];
temp[j + 1] = utf8[i + 1];
byteCount = 2;
break;

case 3:
//这里就开始进行UTF8->Unicode
temp[j + 1] = ((utf8[i] & 0x0F) << 4) | ((utf8[i + 1] >> 2) & 0x0F);
temp[j] = ((utf8[i + 1] & 0x03) << 6) + (utf8[i + 2] & 0x3F);

//取得Unicode的值
memcpy(&unicodeKey, (temp + j), 2);
// printf("unicode key is: 0x%04X\n", unicodeKey);

//根据这个值查表取得对应的GB2312的值
gbKey = SearchCodeTable(unicodeKey);
// printf("gb2312 key is: 0x%04X\n", gbKey);

if (gbKey != 0)
{
//here change the byte
//不为0表示搜索到,将高低两个字节调换调成我要的形式
gbKey = (gbKey >> 8) | (gbKey << 8);
// printf("after changing, gb2312 key is: 0x%04X\n", gbKey);
memcpy((temp + j), &gbKey, 2);
}

byteCount = 3;
break;

case 4:
byteCount = 4;
break;
case 5:
byteCount = 5;
break;
case 6:
byteCount = 6;
break;

default:
printf("the len is more than 6\n");
break;    
        }

        i += byteCount;
        if (byteCount == 1)
        {
j++;
        }
        else
        {
j += 2;
        }

}


fwrite(temp, sizeof(u8),j, fpOut);

delete []utf8;
delete []temp;
fclose(fpIn);
fclose(fpOut);
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: GB2312和UTF-8是两种不同的字符编码标准,它们在字符编码方式、字符集大小、字节数等方面都存在差异。因此,要将GB2312编码的字符串转换为UTF-8编码的字符串,需要进行编码转换。 在C语言中,可以使用iconv库来进行字符集转换。iconv库是一个系统库,可以实现不同编码之间的转换。在使用iconv库之前,需要先启用头文件#include <iconv.h>。 下面是一个示例程序,用于将GB2312编码的字符串转换为UTF-8编码的字符串: ``` #include <stdio.h> #include <iconv.h> #include <stdlib.h> #include <string.h> int main() { char *gb2312_str = "hello world!"; size_t gb2312_len = strlen(gb2312_str); size_t utf8_len = gb2312_len * 3; // UTF-8最多可能会占用3个字节 char *utf8_str = (char *)malloc(utf8_len); // 分配空间 // 初始化iconv_t iconv_t cd = iconv_open("UTF-8", "GB2312"); if (cd == (iconv_t)-1) { printf("初始化iconv_t失败!\n"); exit(1); } // 进行转换 char *inbuf = gb2312_str; char *outbuf = utf8_str; size_t inlen = gb2312_len; size_t outlen = utf8_len; size_t ret = iconv(cd, &inbuf, &inlen, &outbuf, &outlen); if (ret == (size_t)-1) { printf("转换失败!\n"); exit(1); } // 输出UTF-8编码的字符串 printf("UTF-8编码的字符串:%s\n", utf8_str); // 关闭iconv_t iconv_close(cd); free(utf8_str); // 释放空间 return 0; } ``` 在上面的示例程序中,我们使用iconv_t类型表示转换句柄,使用iconv_open()函数来初始化转换句柄。在进行转换时,使用iconv()函数将GB2312编码的字符串转换为UTF-8编码的字符串,并将结果保存在utf8_str中。 最后,使用iconv_close()函数关闭转换句柄,并使用free()函数释放分配的内存空间。 当然,对于实际应用场景,可能还需要进行一些优化和错误处理,例如对输入字符串进行判断、对输出字符串进行截断等等。 ### 回答2: 将GB2312编码转换为UTF-8编码是一种编码转换的过程,可以使用C语言对字符串进行转换实现。具体操作可以如下: 首先需要了解一些基本知识。GB2312编码是双字节编码,每个汉字用两个字节表示;而UTF-8编码是一种变长编码,每个字符根据不同范围的Unicode码用1~4个字节表示。 1. 将GB2312编码的字符串转换为Unicode码。可以使用WideCharToMultiByte()函数将GB2312编码转换为Unicode码,以便进一步处理。 2. 将Unicode码转换为UTF-8编码。可以使用WideCharToMultiByte()函数,设置输出编码格式为UTF-8,即可将Unicode码转换为UTF-8编码。 3. 将UTF-8编码的字符串打印输出。可以使用puts()或printf()函数将字符串输出到屏幕上,确认转换结果是否正确。 需要注意的是,转换过程中可能会出现一些细节问题,如代码页的不同,转换后的字符串长度的变化等。因此,最好在进行转换时,考虑到这些细节问题,避免出现转换错误。 ### 回答3: GB2312和UTF-8都是Unicode编码的变体,但是它们之间是有区别的。GB2312是早期的中文编码,使用双字节表示每个字符,而UTF-8则是一种比较新的编码格式,也是使用变长的字节表示字符,可以表示全球范围内的字符。 在C语言中,我们可以使用iconv函数来进行编码转换。iconv函数的原型如下: ``` #include <iconv.h> size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft); ``` 其中,参数cd是一个iconv_t类型的转换描述符,如果cd等于(iconv_t) -1,则表示转换失败;参数inbuf指向被转换的字符串;参数inbytesleft表示输入的长度,并被函数用于记录转换后余下的需要转换的字节数;参数outbuf指向输出缓冲区,参数outbytesleft表示输出缓冲区的大小并记录转换后余下的缓冲区大小。 我们可以通过下面的代码将GB2312字符串转换为UTF-8字符串。 ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iconv.h> #include <errno.h> int main() { char *src = "你好,世界!"; size_t srclen = strlen(src); size_t dstlen = srclen * 2; char *dst = (char *)malloc(dstlen); memset(dst,0,dstlen); iconv_t cd = iconv_open("UTF-8","GB2312"); if (cd == (iconv_t) -1){ perror("iconv_open"); exit(EXIT_FAILURE); } char *inbuf = src; char *outbuf = dst; if (iconv(cd, (char **)&inbuf, &srclen, &outbuf, &dstlen ) == -1) { perror("iconv"); exit(EXIT_FAILURE); } printf("GB2312: %s\n",src); printf("UTF-8: %s\n",dst); iconv_close(cd); free(dst); return 0; } ``` 在这个例子中,我们首先定义了一个需要转换GB2312字符串,在使用iconv_open函数创建一个iconv_t类型的转换描述符时,我们指定了源编码是GB2312,目标编码是UTF-8。然后,我们再定义一个输出缓冲区,使用iconv函数将GB2312字符串转换为UTF-8字符串。 最后,我们通过printf函数按照不同的编码格式输出了转换后的字符串。需要注意的是,这个例子中的代码并没有对错误进行完整的处理,因此在实际应用时需要注意添加错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值