unicode.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "unicode.h"
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "FileSys.h"
  7. #include "GpsTask.h"
  8. #include "Modem.h"
  9. uint32_t gb2312_unicode_addr;
  10. int g_gb2312_unicodeFileIndex;
  11. //#define GB2312_TO_UNICODE_BASE 0x134000
  12. //#define UNICODE_TO_GB2312_BASE 0x138000
  13. //extern const uint16_t GB2312_UNICODE_MAP[];
  14. void SetCodePara(void)
  15. {
  16. int FileLen;
  17. g_gb2312_unicodeFileIndex=GetFileIndex("gb2312-unicode.uni");
  18. FileLen=GetFileLen(g_gb2312_unicodeFileIndex);
  19. if(FileLen==0)
  20. {
  21. g_gb2312_unicodeFileIndex=-1;
  22. }
  23. }
  24. typedef struct {
  25. uint16_t unicode;
  26. uint16_t gb2312;
  27. } unicode_gb2312_map_t;
  28. extern const unicode_gb2312_map_t UNICODE_GB2312_MAP[];
  29. /*
  30. MIN_CODE = $A1A1;
  31. MAX_CODE = $F7FE;
  32. */
  33. #define GB2312_CODE_ZONE_MIN 0xA1
  34. #define GB2312_CODE_ZONE_MAX 0xF7
  35. #define GB2312_CODE_POS_MIN 0xA1
  36. #define GB2312_CODE_POS_MAX 0xFE
  37. uint16_t
  38. GB2312_to_Unicode(uint16_t c)
  39. {
  40. uint8_t zone, pos;
  41. uint32_t offset;
  42. uint16_t retval;
  43. unsigned char temp[2];
  44. zone = c >> 8;
  45. pos = c & 0xFF;
  46. if ((zone > GB2312_CODE_ZONE_MAX) ||
  47. (zone < GB2312_CODE_ZONE_MIN) ||
  48. (pos > GB2312_CODE_POS_MAX) ||
  49. (pos < GB2312_CODE_POS_MIN))
  50. {
  51. return c;
  52. }
  53. offset = (zone - GB2312_CODE_ZONE_MIN) * (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1) +
  54. (pos - GB2312_CODE_POS_MIN);
  55. //spiFlashBlockRead(GB2312_TO_UNICODE_BASE + offset * 2, (uint8_t *)&retval, 2);
  56. ReadFileData(g_gb2312_unicodeFileIndex,offset*2,2,temp);
  57. retval = temp[0];
  58. retval <<= 8;
  59. retval &= 0xff00;
  60. retval |= temp[1];
  61. //retval=GB2312_UNICODE_MAP[offset*2];
  62. return retval;
  63. }
  64. uint16_t
  65. Unicode_to_GB2312(uint16_t c)
  66. {
  67. int offset;
  68. int low, high, mid;
  69. const unicode_gb2312_map_t *map;
  70. if (((c >> 8) == 0) && ((c & 0xFF) < 0x7F))
  71. {
  72. return c;
  73. }
  74. low = 0;
  75. high = (GB2312_CODE_ZONE_MAX - GB2312_CODE_ZONE_MIN + 1) *
  76. (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1);
  77. while (low <= high)
  78. {
  79. mid = (low + high) / 2;
  80. // spiFlashBlockRead(UNICODE_TO_GB2312_BASE + mid * sizeof(unicode_gb2312_map_t),
  81. // (uint8_t *)&map, sizeof(unicode_gb2312_map_t));
  82. map = & UNICODE_GB2312_MAP[mid];
  83. if (c > map->unicode)
  84. {
  85. low = mid + 1;
  86. }
  87. if (c < map->unicode)
  88. {
  89. high = mid - 1;
  90. }
  91. if (c == map->unicode)
  92. {
  93. return map->gb2312;
  94. }
  95. }
  96. return '?'; // 无法识别,替换为 ?
  97. }
  98. uint16_t
  99. Ansi_to_Unicode(uint16_t *dest, uint16_t size, const uint8_t *src, uint16_t length)
  100. {
  101. uint16_t count = 0;
  102. while((count < size) && length)
  103. {
  104. if ((*src > 0x7F) && (length > 1))
  105. {
  106. dest[count] = GB2312_to_Unicode(((uint16_t)src[0] << 8) | src[1]);
  107. src += 2;
  108. length -= 2;
  109. }
  110. else
  111. {
  112. dest[count] = *src;
  113. src++;
  114. length--;
  115. }
  116. ++count;
  117. }
  118. return count;
  119. }
  120. uint16_t
  121. Unicode_to_Ansi(uint8_t *dest, uint16_t size, const uint8_t *src, uint16_t length)
  122. {
  123. uint16_t count = 0;
  124. uint16_t t;
  125. while((count + 1 < size) && length)
  126. {
  127. t = Unicode_to_GB2312(((uint16_t)src[1] << 8) | src[0]);
  128. if ((t >> 8) == 0)
  129. {
  130. *dest++ = t;
  131. ++count;
  132. }
  133. else
  134. {
  135. *dest++ = t >> 8;
  136. *dest++ = t;
  137. count += 2;
  138. }
  139. src += 2;
  140. length -= 2;
  141. }
  142. *dest++ = 0;
  143. return count;
  144. }
  145. /**************************************************************************************
  146. 将字符形式的Unicode转为Ansi
  147. 比如:字符串“d89ea48ba47fc47e0000”将转为 "默认群组"
  148. ***************************************************************************************/
  149. uint16_t StrUnicodeToAnsi(uint8_t *dest,uint16_t size,const char *src)
  150. {
  151. uint16_t count = 0;
  152. uint16_t t;
  153. int length;
  154. uint16_t v;
  155. char temp[5];
  156. length=strlen(src);
  157. if(length<4) return 0;
  158. memset(dest,0,size);
  159. while((count + 1 < size) && length>0)
  160. {
  161. temp[0]=src[2];temp[1]=src[3];
  162. temp[2]=src[0];temp[3]=src[1];
  163. temp[4]=0;
  164. v=strtol(temp,NULL,16);
  165. t = Unicode_to_GB2312(v);
  166. if ((t >> 8) == 0)
  167. {
  168. *dest++ = t;
  169. ++count;
  170. }
  171. else
  172. {
  173. *dest++ = t >> 8;
  174. *dest++ = t;
  175. count += 2;
  176. }
  177. src += 4;
  178. length -= 4;
  179. }
  180. *dest++ = 0;
  181. return count;
  182. }
  183. /**************************************************************************************
  184. 将字符串转字符串形式的StrUnicode
  185. 比如:字符串"默认群组"(8字节) 转为 “d89ea48ba47fc47e”(16字节)
  186. ***************************************************************************************/
  187. uint16_t AnsiToStrUnicode(uint8_t *dest,uint16_t size,const char *src)
  188. {//"我1" 即0xCED231
  189. uint16_t len,i;
  190. uint16_t temp1,temp2;
  191. uint8_t unicode[4],*ptr;
  192. SUTDS ss;
  193. ptr=(uint8_t *)src;
  194. len = strlen(src);
  195. i=0;
  196. while(1)
  197. {
  198. if(*ptr > 0x9f)
  199. {//
  200. temp1 = *ptr++;
  201. temp1 <<= 8;
  202. temp1 &= 0xff00;
  203. temp1 |= *ptr++;
  204. ss.Data.usData = GB2312_to_Unicode(temp1);
  205. ///////to string
  206. HexToAsc(ss.Data.ucData.b1 ,(char *)dest);dest+=2;
  207. HexToAsc(ss.Data.ucData.b2 ,(char *)dest);dest+=2;
  208. i+=2;
  209. }else{
  210. HexToAsc(*ptr++, (char *)dest);dest+=2;
  211. *dest++=0x30;
  212. *dest++=0x30;
  213. i++;
  214. }
  215. if(i>=size) return 0;
  216. if(i>= len) break;
  217. }
  218. *dest=0;
  219. return 0;
  220. }