苏飞论坛

标题: 十六进制与字符串互转 [打印本页]

作者: qq576733600    时间: 2013-6-8 11:09
标题: 十六进制与字符串互转
//有很多数据 做了十六进制与字符串互转 ,下面告诉下大家 怎么互相转换。

public string StrToHex(string mStr) //返回处理后的十六进制字符串
  {
   return BitConverter.ToString(
   ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " ");
  }
  public string HexToStr(string mHex) // 返回十六进制代表的字符串
  {
   mHex = mHex.Replace(" ", "");
   if (mHex.Length <= 0) return "";
   byte[] vBytes = new byte[mHex.Length / 2];
   for (int i = 0; i < mHex.Length; i += 2)
    if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
     vBytes[i / 2] = 0;
   return ASCIIEncoding.Default.GetString(vBytes);
  }





欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4