123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #include "includes.h"
- #include "unicode.h"
- #include <stdint.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define GB2312_TO_UNICODE_BASE 0x134000 //起始地址?
- #define UNICODE_TO_GB2312_BASE 0x138000
- extern const uint16_t GB2312_UNICODE_MAP[];
- //extern unsigned short giGB2312[21243][2];
- //const unsigned short giGBCount=21243;
- 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;
- // printf("%2x/r",c);
- zone = c >> 8;
- pos = c & 0xFF;
- // printf("%2x-/r",zone);
- // printf("%2x--/r",pos);
- 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);
- retval=GB2312_UNICODE_MAP[offset];//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) //GBK_Unicode
- {
- 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]; //{0x00A4, 0xA1E8},00A4 其实要变成A400 倒过来
- 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字节)默认群组的Unicode就是
- d89ea48ba47fc47e {0x6DF1, 0xC9EE},
- ***************************************************************************************/
- //uint16_t
- //GB_to_UN(uint16_t c)
- //{
- // uint8_t zone, pos;
- // int offset;
- // int low, high, mid;
- // const unicode_gb2312_map_t *map;
- // if (((c >> 8) == 0) && ((c & 0xFF) < 0x7F))
- // {
- // return c;
- // }
- // printf("%2x--/r",c);
- // low = 0;
- // high = (GB2312_CODE_ZONE_MAX - GB2312_CODE_ZONE_MIN + 1) * //8178
- // (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1);
- // while (low <= high)
- // {
- // mid = (low + high) / 2;
- // map = &UNICODE_GB2312_MAP[mid];
- //
- // if (c > map->gb2312)
- // {
- // low = mid + 1;
- // }
- // if (c < map->gb2312)
- // {
- // high = mid - 1;
- // }
- // if (c == map->gb2312)
- // {
- // return map->unicode;
- // }
- // }
- // return '?'; // 无法识别,替换为 ?
- //}
- ///**************************************************************************/
- //uint16_t AnsiToStrUnicode(uint16_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<2) return 0; //因为后面只留2个0 c9eedbdab0eccac2b4a6 00
- // memset(dest,0,size);
- // printf("%2x/n-->",*src);
- //
- // while((count < size) && length>0)
- // {
- // temp[0]=src[0];temp[1]=src[1]; //{0x6DF1, 0xC9EE},
- // temp[2]=src[2];temp[3]=src[3];
- // temp[4]=0;
- // v=strtol(temp,NULL,16);
- // printf("%2x/n->",v);
- // if ((v > 0x7F)&& (length > 1)) //(*src > 0x7F) &&
- // {
- // dest[count] = GB_to_UN(v);
- // printf("%2x/n/r",dest[count]);
- // src += 4;
- // length -= 4;
- // }else{
- // dest[count] = *src;
- // src++;
- // length--;
- //
- // }
- // ++count;
- // }
- //
- // return count;
- //}
- /********************************************************************************/
- uint16_t AnsiToStrUnicode(uint16_t *dest,uint16_t size,const char *src)
- {
- #if 1
- uint16_t count = 0;
- uint16_t t,v;
- int length;
- char temp[5];
-
- unsigned char t1,t2;
- length=strlen(src);
- if(length<2) return 0; //因为后面只留2个0 c9eedbdab0eccac2b4a6 00
-
- memset(dest,0,size); //初始化目标
- while((count < size) && length)
- {
- if(src[0] == 0x30 && src[1] == 0x30) return count;
- temp[0]=src[0];temp[1]=src[1];
- temp[2]=src[2];temp[3]=src[3];
- temp[4]=0;
- v=strtol(temp,NULL,16);
- t1=v&0xff;
- t2=(v>>8)&0xff;
- if((t1 > 0x7F) && (t2 > 0x7f) && (length > 1))
- {
- dest[count] = GB2312_to_Unicode(v);
- src += 4;
- length -= 4;
- }
- else
- {
- t=t2;
- t &= 0xff;
- dest[count] = t;
- src +=2;
- length -=2;
- }
- ++count;
- }
- return 0;
- #else
- uint16_t count = 0;
- uint16_t t,v;
- int length;
- char temp[5];
- length=strlen(src);
- if(length<2) return 0; //因为后面只留2个0 c9eedbdab0eccac2b4a6 00
-
- memset(dest,0,size); //初始化目标
- g_GroupNameLen=0;
- // printf("%2x/n-->",*src);
- while((count < size) && length)
- {
- temp[0]=src[0];temp[1]=src[1];
- temp[2]=src[2];temp[3]=src[3];
- temp[4]=0;
- v=strtol(temp,NULL,16);
- // printf("%2x/n->",v);
- if(v!=0){g_GroupNameLen++;}else{return 0;}
- if ((v > 0x7F)&&((v>>8) > 0x7F) && (length > 1)) // if ((v > 0x7F)&& (length > 1))
- {
- dest[count] = GB2312_to_Unicode(v);
- // printf("%2x/n/r", dest[count]);
- src += 4;
- length -= 4;
- }
- else
- {
- dest[count] = *src;
- src++;
- length--;
- }
- ++count;
- }
- return 0;
- #endif
- }
|