QQ登录

只需一步,快速开始

C#实现编码无关的字符串和字节数组相互转换

[ 复制链接 ]
web应用中,客户端和服务器端需要交换信息,字符串形式的信息交互是主要的一种交换方式。
在信息量较大的情况下,前后台的传递方法使用post方法,后台的接收参数为字节数组。
如果字符串中有中文,客户端在信息发送前需要把它转换为字节数组,转换的时候需要采用指定的编码。
这里分享一种方法,不需要指定编码,是通用的方法。



  1. 1.        字符串转字节数组

  2. private byte[] StringToByteArr(string str)
  3.         {
  4.             List<byte> lst = new List<byte>();
  5.             for (int i = 0; i < str.Length; i++)
  6.             {
  7.                 int c = str[i];
  8.                 if (c >= 0x010000 && c <= 0x10FFFF) {
  9.                     int x  = ((c >> 18) & 0x07) | 0xF0;
  10.                     lst.Add((byte)x);
  11.                     x = ((c >> 12) & 0x3F) | 0x80;
  12.                     lst.Add((byte)x);
  13.                     x = ((c >> 6) & 0x3F) | 0x80;
  14.                     lst.Add((byte)x);
  15.                     x = (c & 0x3F) | 0x80;
  16.                     lst.Add((byte)x);
  17.                 } else if (c >= 0x000800 && c <= 0x00FFFF) {
  18.                     int x = ((c >> 12) & 0x0F) | 0xE0;
  19.                     lst.Add((byte)x);
  20.                     x = ((c >> 6) & 0x3F) | 0x80;
  21.                     lst.Add((byte)x);
  22.                     x = (c & 0x3F) | 0x80;
  23.                     lst.Add((byte)x);
  24.                 } else if (c >= 0x000080 && c <= 0x0007FF) {
  25.                     int x = ((c >> 6) & 0x1F) | 0xC0;
  26.                     lst.Add((byte)x);
  27.                     x = (c & 0x3F) | 0x80;
  28.                     lst.Add((byte)x);
  29.                 } else {
  30.                     lst.Add((byte)(c & 0xFF));
  31.                 }
  32.             }
  33.             byte[] result = new byte[lst.Count];
  34.             for(int i = 0; i < lst.Count; i++){
  35.                 result[i] = lst[i];
  36.             }
  37.             return result;
  38.         }

  39. 2.        字节数组转字符串

  40. private string ByteArrToString(byte[] barr)
  41.         {
  42.             string result = "";
  43.             byte[] arr = barr;
  44.             string  pattern="^1+?(?=0)";
  45.             Regex r = new Regex(pattern);
  46.             Match m = null;
  47.             for (int i = 0; i < arr.Length; i++)
  48.             {
  49.                 string one = Convert.ToString(arr[i], 2);
  50.                 m = r.Match(one);
  51.                 if (m.Success && one.Length == 8)
  52.                 {
  53.                     string v = m.Value;
  54.                     int blth = v.Length;
  55.                     string store = one.Substring(7 - blth);
  56.                     for (int j = 1; j < blth; j++)
  57.                     {
  58.                         store += Convert.ToString(arr[j + i], 2).Substring(2);
  59.                     }
  60.                     result += (char)(Convert.ToInt32(store,2));
  61.                     i += blth - 1;
  62.                 }
  63.                 else
  64.                 {
  65.                     result += (char)arr[i];
  66.                 }
  67.                
  68.             }
  69.             return result;
  70. }
复制代码

回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索