C#中string与byte[]的转换

  1. using System;
  2. using System.Text;
  3. namespace SidleHelper
  4. {
  5.     /// <summary> 
  6.     /// Summary description for StrHelper. 
  7.     /// 命名缩写: 
  8.     /// Str: unicode string 
  9.     /// Arr: unicode array 
  10.     /// Hex: 二进制数据 
  11.     /// Hexbin: 二进制数据用ASCII字符表示 例 字符'1'的hex是0x31表示为hexbin是 '3''1' 
  12.     /// Asc: ASCII 
  13.     /// Uni: UNICODE 
  14.     /// </summary> 
  15.     public sealed class StrHelper
  16.     {
  17.         #region Hex与Hexbin的转换
  18.         public static void Hexbin2Hex(byte[] bHexbin, byte[] bHex, int nLen)
  19.         {
  20.             for (int i = 0; i < nLen / 2; i++)
  21.             {
  22.                 if (bHexbin[2 * i] < 0x41)
  23.                 {
  24.                     bHex[i] = Convert.ToByte(((bHexbin[2 * i] - 0x30) << 4) & 0xf0);
  25.                 }
  26.                 else
  27.                 {
  28.                     bHex[i] = Convert.ToByte(((bHexbin[2 * i] - 0x37) << 4) & 0xf0);
  29.                 }
  30.                 if (bHexbin[2 * i + 1] < 0x41)
  31.                 {
  32.                     bHex[i] |= Convert.ToByte((bHexbin[2 * i + 1] - 0x30) & 0x0f);
  33.                 }
  34.                 else
  35.                 {
  36.                     bHex[i] |= Convert.ToByte((bHexbin[2 * i + 1] - 0x37) & 0x0f);
  37.                 }
  38.             }
  39.         }
  40.         public static byte[] Hexbin2Hex(byte[] bHexbin, int nLen)
  41.         {
  42.             if (nLen % 2 != 0)
  43.                 return null;
  44.             byte[] bHex = new byte[nLen / 2];
  45.             Hexbin2Hex(bHexbin, bHex, nLen);
  46.             return bHex;
  47.         }
  48.         public static void Hex2Hexbin(byte[] bHex, byte[] bHexbin, int nLen)
  49.         {
  50.             byte c;
  51.             for (int i = 0; i < nLen; i++)
  52.             {
  53.                 c = Convert.ToByte((bHex[i] >> 4) & 0x0f);
  54.                 if (c < 0x0a)
  55.                 {
  56.                     bHexbin[2 * i] = Convert.ToByte(c + 0x30);
  57.                 }
  58.                 else
  59.                 {
  60.                     bHexbin[2 * i] = Convert.ToByte(c + 0x37);
  61.                 }
  62.                 c = Convert.ToByte(bHex[i] & 0x0f);
  63.                 if (c < 0x0a)
  64.                 {
  65.                     bHexbin[2 * i + 1] = Convert.ToByte(c + 0x30);
  66.                 }
  67.                 else
  68.                 {
  69.                     bHexbin[2 * i + 1] = Convert.ToByte(c + 0x37);
  70.                 }
  71.             }
  72.         }
  73.         public static byte[] Hex2Hexbin(byte[] bHex, int nLen)
  74.         {
  75.             byte[] bHexbin = new byte[nLen * 2];
  76.             Hex2Hexbin(bHex, bHexbin, nLen);
  77.             return bHexbin;
  78.         }
  79.         #endregion
  80.         #region 数组和字符串之间的转化
  81.         public static byte[] Str2Arr(String s)
  82.         {
  83.             return (new UnicodeEncoding()).GetBytes(s);
  84.         }
  85.         public static string Arr2Str(byte[] buffer)
  86.         {
  87.             return (new UnicodeEncoding()).GetString(buffer, 0, buffer.Length);
  88.         }
  89.         public static byte[] Str2AscArr(String s)
  90.         {
  91.             return System.Text.UnicodeEncoding.Convert(System.Text.Encoding.Unicode,
  92.             System.Text.Encoding.ASCII,
  93.             Str2Arr(s));
  94.         }
  95.         public static byte[] Str2HexAscArr(String s)
  96.         {
  97.             byte[] hex = Str2AscArr(s);
  98.             byte[] hexbin = Hex2Hexbin(hex, hex.Length);
  99.             return hexbin;
  100.         }
  101.         public static string AscArr2Str(byte[] b)
  102.         {
  103.             return System.Text.UnicodeEncoding.Unicode.GetString(
  104.             System.Text.ASCIIEncoding.Convert(System.Text.Encoding.ASCII,
  105.             System.Text.Encoding.Unicode,
  106.             b)
  107.             );
  108.         }
  109.         public static string HexAscArr2Str(byte[] buffer)
  110.         {
  111.             byte[] b = Hex2Hexbin(buffer, buffer.Length);
  112.             return AscArr2Str(b);
  113.         }
  114.         #endregion
  115.     }
  116. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值