#include "unicode.h" #include #include #include #include #include "FileSys.h" #include "GpsTask.h" #include "Modem.h" uint32_t gb2312_unicode_addr; int g_gb2312_unicodeFileIndex; //#define GB2312_TO_UNICODE_BASE 0x134000 //#define UNICODE_TO_GB2312_BASE 0x138000 //extern const uint16_t GB2312_UNICODE_MAP[]; void SetCodePara(void) { int FileLen; g_gb2312_unicodeFileIndex=GetFileIndex("gb2312-unicode.uni"); FileLen=GetFileLen(g_gb2312_unicodeFileIndex); if(FileLen==0) { g_gb2312_unicodeFileIndex=-1; } } typedef struct { uint16_t unicode; uint16_t gb2312; } unicode_gb2312_map_t; extern const unicode_gb2312_map_t UNICODE_GB2312_MAP[]; /* MIN_CODE = $A1A1; MAX_CODE = $F7FE; */ #define GB2312_CODE_ZONE_MIN 0xA1 #define GB2312_CODE_ZONE_MAX 0xF7 #define GB2312_CODE_POS_MIN 0xA1 #define GB2312_CODE_POS_MAX 0xFE uint16_t GB2312_to_Unicode(uint16_t c) { uint8_t zone, pos; uint32_t offset; uint16_t retval; unsigned char temp[2]; zone = c >> 8; pos = c & 0xFF; if ((zone > GB2312_CODE_ZONE_MAX) || (zone < GB2312_CODE_ZONE_MIN) || (pos > GB2312_CODE_POS_MAX) || (pos < GB2312_CODE_POS_MIN)) { return c; } offset = (zone - GB2312_CODE_ZONE_MIN) * (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1) + (pos - GB2312_CODE_POS_MIN); //spiFlashBlockRead(GB2312_TO_UNICODE_BASE + offset * 2, (uint8_t *)&retval, 2); ReadFileData(g_gb2312_unicodeFileIndex,offset*2,2,temp); retval = temp[0]; retval <<= 8; retval &= 0xff00; retval |= temp[1]; //retval=GB2312_UNICODE_MAP[offset*2]; return retval; } uint16_t Unicode_to_GB2312(uint16_t c) { int offset; int low, high, mid; const unicode_gb2312_map_t *map; if (((c >> 8) == 0) && ((c & 0xFF) < 0x7F)) { return c; } low = 0; high = (GB2312_CODE_ZONE_MAX - GB2312_CODE_ZONE_MIN + 1) * (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1); while (low <= high) { mid = (low + high) / 2; // spiFlashBlockRead(UNICODE_TO_GB2312_BASE + mid * sizeof(unicode_gb2312_map_t), // (uint8_t *)&map, sizeof(unicode_gb2312_map_t)); map = & UNICODE_GB2312_MAP[mid]; if (c > map->unicode) { low = mid + 1; } if (c < map->unicode) { high = mid - 1; } if (c == map->unicode) { return map->gb2312; } } return '?'; // 无法识别,替换为 ? } uint16_t Ansi_to_Unicode(uint16_t *dest, uint16_t size, const uint8_t *src, uint16_t length) { uint16_t count = 0; while((count < size) && length) { if ((*src > 0x7F) && (length > 1)) { dest[count] = GB2312_to_Unicode(((uint16_t)src[0] << 8) | src[1]); src += 2; length -= 2; } else { dest[count] = *src; src++; length--; } ++count; } return count; } uint16_t Unicode_to_Ansi(uint8_t *dest, uint16_t size, const uint8_t *src, uint16_t length) { uint16_t count = 0; uint16_t t; while((count + 1 < size) && length) { t = Unicode_to_GB2312(((uint16_t)src[1] << 8) | src[0]); if ((t >> 8) == 0) { *dest++ = t; ++count; } else { *dest++ = t >> 8; *dest++ = t; count += 2; } src += 2; length -= 2; } *dest++ = 0; return count; } /************************************************************************************** 将字符形式的Unicode转为Ansi 比如:字符串“d89ea48ba47fc47e0000”将转为 "默认群组" ***************************************************************************************/ uint16_t StrUnicodeToAnsi(uint8_t *dest,uint16_t size,const char *src) { uint16_t count = 0; uint16_t t; int length; uint16_t v; char temp[5]; length=strlen(src); if(length<4) return 0; memset(dest,0,size); while((count + 1 < size) && length>0) { temp[0]=src[2];temp[1]=src[3]; temp[2]=src[0];temp[3]=src[1]; temp[4]=0; v=strtol(temp,NULL,16); t = Unicode_to_GB2312(v); if ((t >> 8) == 0) { *dest++ = t; ++count; } else { *dest++ = t >> 8; *dest++ = t; count += 2; } src += 4; length -= 4; } *dest++ = 0; return count; } /************************************************************************************** 将字符串转字符串形式的StrUnicode 比如:字符串"默认群组"(8字节) 转为 “d89ea48ba47fc47e”(16字节) ***************************************************************************************/ uint16_t AnsiToStrUnicode(uint8_t *dest,uint16_t size,const char *src) {//"我1" 即0xCED231 uint16_t len,i; uint16_t temp1,temp2; uint8_t unicode[4],*ptr; SUTDS ss; ptr=(uint8_t *)src; len = strlen(src); i=0; while(1) { if(*ptr > 0x9f) {// temp1 = *ptr++; temp1 <<= 8; temp1 &= 0xff00; temp1 |= *ptr++; ss.Data.usData = GB2312_to_Unicode(temp1); ///////to string HexToAsc(ss.Data.ucData.b1 ,(char *)dest);dest+=2; HexToAsc(ss.Data.ucData.b2 ,(char *)dest);dest+=2; i+=2; }else{ HexToAsc(*ptr++, (char *)dest);dest+=2; *dest++=0x30; *dest++=0x30; i++; } if(i>=size) return 0; if(i>= len) break; } *dest=0; return 0; }