unicode.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "unicode.h"
  2. #include "GB2312UnicodeTable.h"
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. /*
  8. MIN_CODE = $A1A1;
  9. MAX_CODE = $F7FE;
  10. */
  11. #define GB2312_CODE_ZONE_MIN 0xA1
  12. #define GB2312_CODE_ZONE_MAX 0xF7
  13. #define GB2312_CODE_POS_MIN 0xA1
  14. #define GB2312_CODE_POS_MAX 0xFE
  15. static uint16_t
  16. GB2312_to_Unicode(uint16_t c)
  17. {
  18. uint8_t zone, pos;
  19. uint32_t offset;
  20. uint16_t retval;
  21. zone = c >> 8;
  22. pos = c & 0xFF;
  23. if ((zone > GB2312_CODE_ZONE_MAX) ||
  24. (zone < GB2312_CODE_ZONE_MIN) ||
  25. (pos > GB2312_CODE_POS_MAX) ||
  26. (pos < GB2312_CODE_POS_MIN))
  27. {
  28. return c;
  29. }
  30. offset = (zone - GB2312_CODE_ZONE_MIN) * (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1) +
  31. (pos - GB2312_CODE_POS_MIN);
  32. retval=GB2312_UNICODE_MAP[offset];//offset*2
  33. return retval;
  34. }
  35. uint16_t
  36. Ansi_to_Unicode(uint16_t *dest, uint16_t size, const uint8_t *src, uint16_t length) //GBK_Unicode
  37. {
  38. uint16_t count = 0;
  39. uint16_t temp,tmp;
  40. while((count < size) && length){
  41. if ((*src > 0x7F) && (length > 1)){
  42. dest[count] = GB2312_to_Unicode(((uint16_t)src[0] << 8) | src[1]);
  43. src += 2;
  44. length -= 2;
  45. }
  46. else
  47. {
  48. dest[count] = *src;
  49. src++;
  50. length--;
  51. }
  52. ++count;
  53. }
  54. return count;
  55. }
  56. ///////////////////////////////////////////////
  57. static uint16_t
  58. Unicode_to_GB2312(uint16_t c)
  59. {
  60. int offset;
  61. int low, high, mid;
  62. //const unicode_gb2312_map_t *map;
  63. unicode_gb2312_map_t map;
  64. if (((c >> 8) == 0) && ((c & 0xFF) < 0x7F))
  65. {
  66. return c;
  67. }
  68. low = 0;
  69. high = (GB2312_CODE_ZONE_MAX - GB2312_CODE_ZONE_MIN + 1) *
  70. (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1);
  71. while (low <= high)
  72. {
  73. mid = (low + high) / 2;
  74. // spiFlashBlockRead(UNICODE_TO_GB2312_BASE + mid * sizeof(unicode_gb2312_map_t),
  75. // (uint8_t *)&map, sizeof(unicode_gb2312_map_t));
  76. map = UNICODE_GB2312_MAP[mid];
  77. //ReadFileData(int FileIndex,u32 startAddr,u32 readLen,unsigned char *pData)
  78. //ReadFileData(UniToGBKIndex, mid * sizeof(unicode_gb2312_map_t),sizeof(unicode_gb2312_map_t),(uint8_t *)&map);
  79. if (c > map.unicode)
  80. {
  81. low = mid + 1;
  82. }
  83. if (c < map.unicode)
  84. {
  85. high = mid - 1;
  86. }
  87. if (c == map.unicode)
  88. {
  89. return map.gb2312;
  90. }
  91. }
  92. return '?'; // ÎÞ·¨Ê¶±ð£¬Ì滻Ϊ ?
  93. }
  94. uint16_t
  95. StrUnicodeToAnsi(uint8_t *dest, uint16_t size, const char *src){
  96. uint16_t count = 0;
  97. uint16_t t;
  98. int length;
  99. uint16_t v;
  100. char temp[5];
  101. length=strlen(src);
  102. if(length<4) return 0;
  103. // memset(dest,0,size);
  104. while((count + 1 < size) && length>0)
  105. {
  106. // temp[0]=src[2];temp[1]=src[3]; //{0x00A4, 0xA1E8},00A4 ÆäʵҪ±ä³ÉA400 µ¹¹ýÀ´
  107. // temp[2]=src[0];temp[3]=src[1];
  108. // temp[4]=0;
  109. temp[0]=src[0];temp[1]=src[1]; //{0x00A4, 0xA1E8},00A4 ÆäʵҪ±ä³ÉA400 µ¹¹ýÀ´
  110. temp[2]=src[2];temp[3]=src[3];
  111. temp[4]=0;
  112. v=strtol(temp,NULL,16);
  113. t = Unicode_to_GB2312(v);
  114. if ((t >> 8) == 0)
  115. {
  116. *dest++ = t;
  117. ++count;
  118. }
  119. else
  120. {
  121. *dest++ = t >> 8;
  122. *dest++ = t;
  123. count += 2;
  124. }
  125. src += 4;
  126. length -= 4;
  127. }
  128. *dest++ = 0;
  129. return count;
  130. }