您好,欢迎来到年旅网。
搜索
您的当前位置:首页在C#语言中十六进制字符串与字节型数组的相互转换

在C#语言中十六进制字符串与字节型数组的相互转换

来源:年旅网

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_336_280.htm" frameborder="0" width="340" scrolling="no" height="282">

由于用到了十六进制字符串与字节型数组的相互转换,前一段时间从网上看到了一个中有这么一个转换函数,但是在调试过程中出现错误,经过测试最终修改为以下:

 class HexCon
 {
  // 把字节型转换成十六进制字符串,最终不带有“0x”标志。

  public static string ByteToString(byte[] InBytes)
  {
   string StringOut="";
   foreach (byte InByte in InBytes)
   {
    StringOut=StringOut + String.Format("{0:X2}",InByte) + " ";
   }
   
   return StringOut.Trim();
  }

//把十六进制字符串转换成字节型。

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

  public static byte[] StringToByte(string InString)
  {
   string[] ByteStrings;
   int      TmpIndex;
   ByteStrings = InString.Split(" ".ToCharArray());
   byte[] ByteOut;
   ByteOut = new byte[ByteStrings.Length];
   for (int i = 0;i<=ByteStrings.Length-1;i++)
   {
    TmpIndex = ByteStrings[i].ToLower().IndexOf("0x");
    if (TmpIndex >= 0)
    {
     ByteOut[i] = byte.Parse(ByteStrings[i].Remove(TmpIndex,2),System.Globalization.NumberStyles.HexNumber);
    }
    else
    {
     ByteOut[i] = byte.Parse(ByteStrings[i],System.Globalization.NumberStyles.HexNumber);
    }
   } 
   return ByteOut;
  }
 }

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_ads_468_15.htm" frameborder="0" width="470" scrolling="no" height="18">

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_syndication_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

搜索其它相关资源,请用Google搜索:
border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_search_40.htm" frameborder="0" width="460" scrolling="no" height="40">

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务