+ -
当前位置:首页 → 问答吧 → 大侠帮看下转unicode的问题

大侠帮看下转unicode的问题

时间:2010-07-26

来源:互联网

  1. /*utf8转unicode*/
  2. int translateUtfToUnicode(char *str,char *res)   /*src is source address ,res is destination address*/
  3. {
  4.         static char buf[32];
  5.         char data[100];
  6.         char temp1;   /*save the char*/
  7.         char temp2[3];   /*save the %x of the char*/
  8.         int i=0;
  9.         memset(data,'\0',sizeof(data));
  10.         memset(res,'\0',sizeof(res));
  11.         while(i<strlen(str))/*this code translate utf to unicode*/
  12.         {
  13.            if(!(str[i]&0x080))/*just ascii ,1 utf code*/
  14.            {
  15.                 strcat(data,"00");
  16.                 temp1=str[i++]&0x07F;
  17.                 sprintf(temp2,"%2X",temp1&0x0ff);
  18.                 temp2[2]='\0';
  19.                 strcat(data,temp2);
  20.            }else if( !( (str[i]&0x0e0)&~0x0c0 ) ){/*just 2 utf code*/
  21.           
  22.                 //printf("u 2\n");
  23.                 temp1=(str[i]&0x01f)>>2;
  24.                 sprintf(temp2,"%2x",temp1&0x0ff);
  25.                 temp2[2]='\0';
  26.                 strcat(data,temp2);      
  27.                 temp1=str[i++]<<6;
  28.                 temp1|=(str[i++]&0x03f);
  29.                 sprintf(temp2,"%2x",temp1&0x0ff);
  30.                 temp2[2]='\0';
  31.                 strcat(data,temp2);
  32.            }else if( !( (str[i]&0x0f0)&~0x0e0 ) ){/*just 3 utf code*/
  33.                         temp1=(str[i++]<<4)&0x0f0;
  34.                         temp1|=((str[i]&0x0ff)>>2)&0x00f;
  35.                         sprintf(temp2,"%2X",temp1&0x0ff);
  36.                         temp2[2]='\0';
  37.                         strcat(data,temp2);
  38.                   
  39.                         temp1=(str[i++]<<6)&0x0c0;
  40.                         temp1|=str[i++]&0x03f;
  41.                         sprintf(temp2,"%2X",temp1&0x0ff);
  42.                         temp2[2]='\0';
  43.                         strcat(data,temp2);
  44.            }else{
  45.                         break;
  46.            }
  47.         }/*while*/
  48.          
  49.          for(i=0;i<strlen((char*)&data);i++)
  50.          {
  51.                  if(i%4==0)
  52.                  {
  53.                          sprintf(buf,"%c",data[i]);
  54.                          strcat(res,"\\u");
  55.                          strcat(res,(char*)&buf);        
  56.                  }else{
  57.                          sprintf(buf,"%c",data[i]);
  58.                          strcat(res,(char*)&buf);
  59.                  }
  60.          }
  61. }
复制代码
translateUtfToUnicode("黄龙9",res) ;
输出\u9EC4\u9F99\u0039 而不是我想要的\u9ec4\u9f999这种格式?有高手帮忙解决下么

作者: jd808   发布时间: 2010-07-26

帮你顶,有现成的函数吧。
待高人。

作者: ecjtubaowp   发布时间: 2010-07-26

相关阅读 更多