因iconv方法有些编译器不支持,则采用下面映射方法
完全代码参考:https://download.csdn.net/download/weixin_55163060/84566848
unsigned short giGB2312[21243][2];
const unsigned short giGBCount=21243;
/*
Unicode符号范围 | UTF-8编码方式
(十六进制) | (二进制)
----------------------+---------------------------------------------
0000 0000-0000 007F | 0xxxxxxx
0000 0080-0000 07FF | 110xxxxx 10xxxxxx
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
/**
* @brief utf8编码转unicode字符集(usc4),最大支持4字节utf8编码,(4字节以上在中日韩文中为生僻字)
* @param *utf8 utf8变长编码字节集1~4个字节
* @param *unicode utf8编码转unicode字符集结果,最大4个字节,返回的字节序与utf8编码序一致
* @return length 0:utf8解码异常,others:本次utf8编码长度
*/
unsigned char UTF8ToUnicode(unsigned char *utf8, unsigned int *unicode) {
const unsigned char lut_size = 3;
const unsigned char length_lut[] = {2, 3, 4};
const unsigned char range_lut[] = {0xE0, 0xF0, 0xF8};
const unsigned char mask_lut[] = {0x1F, 0x0F, 0x07};
unsigned char length = 0;
char b = *(utf8 + 0);
unsigned int i = 0;
if(utf8 == NULL) {
*unicode = 0;
return -1;
}
// utf8编码兼容ASCII编码,使用0xxxxxx 表示00~7F
if(b < 0x80) {
*unicode = b;
return 1;
}
// utf8不兼容ISO8859-1 ASCII拓展字符集
// 同时最大支持编码6个字节即1111110X
if(b < 0xC0 || b > 0xFD) {
*unicode = 0;
return -1;
}
for(i = 0; i < lut_size; i++) {
if(b < range_lut[i]) {
*unicode = b & mask_lut[i];
length = length_lut[i];
break;
}
}
// 超过四字节的utf8编码不进行解析
if(length == 0) {
*unicode = 0;
return -1;
}
// 取后续字节数据
for(i = 1; i < length; i++ ) {
b = *(utf8 + i);
// 多字节utf8编码后续字节范围10xxxxxx~10111111
if( b < 0x80 || b > 0xBF ) {
break;
}
*unicode <<= 6;
// 00111111
*unicode |= (b & 0x3F);
}
// 长度校验
return (i < length) ? -1 : length;
}
unsigned short UnicodeToGB2312 (int unicode, unsigned short *gb2312)
{
int i;
if(unicode<0x80)
{
*gb2312 = unicode;
return 1;
}
for(i=0;i<giGBCount;i++)
{
if (giGB2312[i][1]==unicode)
*gb2312 = giGB2312[i][0];
}
return 2;
}
int get_size_from_utf8(unsigned char ch)
{
int count;
if ((ch & 0x80) == 0)
//这是用来兼容ASSIC的。也就是说一个标准的ASSIC文件就是UTF-8文件
count = 1;
else if ((ch & 0xE0) == 0xC0)
//0xE0是11100000
//0xC0是11000000
//是说前面3个bit 是110 ,那当前的字符和后面的一个字符组成一个汉字
count = 2;
else if ((ch & 0xF0) == 0xE0)
//0xF0是11110000
//0xE0是11100000
//是说前面4个bit 是1110 ,那当前的字符和后面的两个字符组成一个汉字
count = 3;
else if ((ch & 0xF8) == 0xF0)
count = 4;
else if ((ch & 0xFC) == 0xF8)
count = 5;
else if ((ch & 0xFE) == 0xFC)
count = 6;
else
count = 1;
return count;
}
int utf8togb2312(char *utf8str, int utf8len, char *gb2312str, int gb2312len)
{
unsigned int unicode = 0;
int i=0;
gb2312len = 0;
for(i=0; i<utf8len;)
{
unsigned char *str = (unsigned char *)&utf8str[i];
int bytes_utf8 = get_size_from_utf8(str[0]);
int bytes_gb2312 = 0;
UTF8ToUnicode(str, &unicode);
printf("unicode=0x%04x\n", unicode);
bytes_gb2312 = UnicodeToGB2312(unicode, (unsigned short *)&gb2312str[gb2312len]);
printf("bytes=%d, %02x, gb2312len=%d, %02x\n", bytes_utf8, str[0], gb2312len, *((short*)&gb2312str[gb2312len]));
gb2312len += bytes_gb2312;
i += bytes_utf8;
}
return 0;
}
unsigned short giGB2312[21243][2]={
{0xa1a1,0x3000},{0xa2a1,0x3001},{0xa3a1,0x3002},{0xa8a1,0x3003},{0xa9a1,0x3005},
{0x65a9,0x3006},{0x96a9,0x3007},{0xb4a1,0x3008},{0xb5a1,0x3009},{0xb6a1,0x300a},
... ...