Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1562093
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: 嵌入式

2012-06-19 11:06:16

debug.h

点击(此处)折叠或打开

  1. #ifndef __DEBUG_H__
  2. #define __DEBUG_H__

  3. #define DEBUG 1

  4. #if DEBUG
  5. #include <stdio.h>
  6. #include <stdarg.h>

  7. #define DebugWhere() printf("FILE:%s,LINE:%d,FUNC:%s\n",\
  8.         __FILE__,__LINE__,__FUNCTION__)
  9. static inline void debug_p(const char *format,...)
  10. {
  11.     va_list args;

  12.     va_start(args,format);
  13.     vprintf(format,args);
  14.     va_end(args);
  15. }
  16. #else
  17. #define DebugWhere()

  18. static inline void debug_p(const char *format,...)
  19. {
  20. }

  21. #endif

  22. #endif



main.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "debug.h"
  5. /*
  6.  * utf8编码转换为unicode编码
  7.  * 输入:源字符串,目的字符串,数据长度
  8.  *
  9.  */

  10. static int my_utf8_to_unicode(unsigned char* unicode, unsigned char* utf8, int len)
  11. {
  12.     int length=0;
  13.     unsigned int code_int;
  14.     int i;
  15.     
  16.     debug_p("len = %d\n",len);
  17.     for(i = 0; i < len ; i++) {
  18.         if(utf8[i] <= 0x7f) {
  19.             unicode[length] = 0x00;
  20.             length++;
  21.             unicode[length] = utf8[i];
  22.             length++;    
  23.         }
  24.         else if(utf8[i] <= 0xdf) {
  25.             code_int = (((unsigned char) utf8[i] & 0x1f) << 6) + ((unsigned char) utf8[i+1] & 0x3f) ;
  26.             unicode[length] = (unsigned char) (code_int >> 8);
  27.             i +=1;
  28.             unicode[length+1] = (unsigned char) code_int;
  29.             unicode[length] = (unsigned char) (code_int >> 8);
  30.             length += 2;
  31.         } else {
  32.             code_int = ((int) ((unsigned char) utf8[i] & 0x0f) << 12)+(((unsigned char) utf8[i+1] & 0x3f) << 6)+
  33.                 ((unsigned char) utf8[i+2] & 0x3f);
  34.             i +=2;
  35.             unicode[length+1] = (unsigned char) code_int;
  36.             unicode[length] = (unsigned char) (code_int >> 8);
  37.             length += 2;
  38.         }
  39.     }
  40.     debug_p("length = %d\n",length);

  41.     return (length);
  42. }

  43. int main()
  44. {
  45.     unsigned char unicode[128];
  46.     unsigned char *utf8_11 = "工作愉快";
  47.     unsigned char *utf8_1 = "工";
  48.     unsigned char *utf8_2 = "作";
  49.     unsigned char *utf8_3 = "愉";
  50.     unsigned char *utf8_4 = "快";
  51.     unsigned char buf[3] = {0};
  52.     unsigned char buff[128] = {0};

  53.     int i,len = 0;

  54.     memset(unicode,0,sizeof(unicode));
  55.     len = strlen(utf8_11);
  56.     len = my_utf8_to_unicode(unicode,utf8_11,len);
  57.     for(i = 0 ; i < len ; i++){
  58.         printf("unicode[%d] = %02x\n",i,unicode[i]);
  59.         sprintf(buf,"%02x",unicode[i]);
  60.         strcat(buff,buf);
  61.     }
  62.     printf("hello: %s\n",buff);

  63.     len = my_utf8_to_unicode(unicode,utf8_1,2);
  64.     for(i = 0 ; i < len ; i++){
  65.         printf("unicode[%d] = %02x\n",i,(unsigned char)unicode[i]);
  66.     }

  67.     len = my_utf8_to_unicode(unicode,utf8_4,2);
  68.     for(i = 0 ; i < len ; i++){
  69.         printf("unicode[%d] = %02x\n",i,(unsigned char)unicode[i]);
  70.     }

  71. }

阅读(2537) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~