手机短信字符编码的问题

这几个月作手机短信的项目,碰到了很多字符编码的问题,真头痛。经过多番资料的搜索、亲手尝试和高人的指点,现在好像没那么迷惘了。现作了一些总结跟大家分享(有误之处请指点)   
    
      首先应该把字节数组看成是String的载体。   
      dot   Net使用的字符串String是Unicode编码的;它也是以Unicode编码的形式显示字符串。   
    
      以下是用自己语言对几个常用函数的说明:   
      (自己总结的,反正看不明MSDN)   
      bytes=System.Text.Encoding.Unicode.GetBytes(str)   
      作用:把str的载体作Unicode->Unicode的编码转换--也就是没有对载体作任何的转换。因些使用此函数可以得代表该String载体的字节数组。   
      str=System.Text.Encoding.Unicode.GetString(bytes)   
      作用:对字节数组作Unicode->Unicode的编码转换--即没有转换,把经过转换后的字节数组作为str的载体。   
    
      bytes=System.Text.Encoding.Utf8.GetBytes(str)   
      作用:把str的载体作Utf8->Unicode的编码转换。返回的是经过转换后的字符数组   
      str=System.Text.Encoding.Utf8.GetString(bytes)   
      作用:对字节数组作Gb2312->Unicode的编码转换,把经过转换后的字节数组作为str的载体。   
    
      bytes=System.Text.Encoding.GetEncoding("GB2312").GetBytes(str)   
      作用:把str的载体作Gb2312->Unicode的编码转换。返回的是经过转换后的字符数组   
      str=System.Text.Encoding.GetEncoding("GB2312").GetString(bytes)   
      作用:对字节数组作Gb2312->Unicode的编码转换,把经过转换后的字节数组作为str的载体。   
    
  如此类推   
      bytes=System.Text.Encoding.GetEncoding("XXX").GetBytes(str)   
      作用:把str的载体作XXX->Unicode的编码转换。返回的是经过转换后的字符数组   
      str=System.Text.Encoding.GetEncoding("XXX").GetString(bytes)   
      作用:对字节数组作XXX->Unicode的编码转换,把经过转换后的字节数组作为str的载体。   
    
  这里是我收集的一些有关字符编码的资料:http://61.145.116.154/bm/   
  还有:   
  http://www.unicode.org/charts/unihan.html   
  根据Unicode编码查其对应字符的字形、Utf8、汉字区位码等   
  http://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/GB/GB2312.TXT   Unicode与Gb2312的对照表   
  http://www.sun.com/developers/gadc/technicalpublications/articles/mabiao.txt   
  Unicode与Gbk对照表

//     string   a= "\xb4\x41\x23";   
//     byte[]   b= new   byte[3];   
//     b[0]=(byte)a[0];   
//     b[1]=(byte)a[1];   
//     b[2]=(byte)a[2];
//
//     string   a= "\xb4\x41\x23";   
//     byte[]   b= Encoding.ASCII.GetBytes(a);  
//
//     byte[]   =   System.Text.Encoding.ASCII.GetBytes(string)     
//     相反:string   -->byte[]   
//     string   =   System.Text.Encoding.Asscii.GetString(byte[]);   
//
//     string   strTemp   =   "我abc";   
//     for(int   i   =   0;   i   <   strTemp.Length;   i   ++)   
//     {   
//         string   sTemp   =   strTemp.Substring(i,   1);   
//         int   iLen   =   System.Text.Encoding.Default.GetByteCount(sTemp);   
//         if(iLen   ==   2)   
//         {   
//             //双字节   
//         }   
//         else   
//         {   
//             //单字节   
//         }   
//     }
//
//     char   ch="a";   
//     if((int)ch>255)   
//     {   
//         //双字节   
//     }   
//     else   
//     {   
//         //单字节   
//   }


//     CString strText;//中间字符串   
//     strText.Format("%d",byLenth);//10进制->中间字符串
//     sscanf(strText,"%x",&temp);//中间字符串->16进制数据

//     char   *pData   =   new   char   [10]   ;   
//     CString   strData2   ;   
// //    int   d   =   32   ;   
//     sprintf(pData,"%02x",byLenth)   ;   
//     strData2   =   pData   ;   
//     delete   []pData   ;

char     是一个字符,如果用来表示数值的话,能表示-128--127之间   
  byte     是无符号   char     如果用来表示数值的话,能表示   0-255之间 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值