#include "unicode.h" #include "GB2312UnicodeTable.h" #include #include #include #include /* 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 static uint16_t GB2312_to_Unicode(uint16_t c) { uint8_t zone, pos; uint32_t offset; uint16_t retval; 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); retval=GB2312_UNICODE_MAP[offset];//offset*2 return retval; } uint16_t Ansi_to_Unicode(uint16_t *dest, uint16_t size, const uint8_t *src, uint16_t length) //GBK_Unicode { uint16_t count = 0; uint16_t temp,tmp; 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; } /////////////////////////////////////////////// static uint16_t Unicode_to_GB2312(uint16_t c) { int offset; int low, high, mid; //const unicode_gb2312_map_t *map; 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]; //ReadFileData(int FileIndex,u32 startAddr,u32 readLen,unsigned char *pData) //ReadFileData(UniToGBKIndex, mid * sizeof(unicode_gb2312_map_t),sizeof(unicode_gb2312_map_t),(uint8_t *)&map); if (c > map.unicode) { low = mid + 1; } if (c < map.unicode) { high = mid - 1; } if (c == map.unicode) { return map.gb2312; } } return '?'; // ÎÞ·¨Ê¶±ð£¬Ì滻Ϊ ? } 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]; //{0x00A4, 0xA1E8},00A4 ÆäʵҪ±ä³ÉA400 µ¹¹ýÀ´ // temp[2]=src[0];temp[3]=src[1]; // temp[4]=0; temp[0]=src[0];temp[1]=src[1]; //{0x00A4, 0xA1E8},00A4 ÆäʵҪ±ä³ÉA400 µ¹¹ýÀ´ temp[2]=src[2];temp[3]=src[3]; 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; }