unicode.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. }