C# 16进制类型转换

转载连接: http://blog.csdn.net/nieweiking/article/details/8206460


  1. /// <summary>  
  2.         /// 字节数组转16进制字符串  
  3.         /// </summary>  
  4.         /// <param name="bytes"></param>  
  5.         /// <returns></returns>  
  6.         public static string byteToHexStr(byte[] bytes)  
  7.         {  
  8.             string returnStr = "";  
  9.             if (bytes != null)  
  10.             {  
  11.                 for (int i = 0; i < bytes.Length; i++)  
  12.                 {  
  13.                     returnStr += bytes[i].ToString("X2");  
  14.                 }  
  15.             }  
  16.             return returnStr;  
  17.         }  

[csharp]  view plain  copy
  1. /// <summary>  
  2.         /// 字符串转16进制字节数组  
  3.         /// </summary>  
  4.         /// <param name="hexString"></param>  
  5.         /// <returns></returns>  
  6.         private static byte[] strToToHexByte(string hexString)  
  7.         {  
  8.             hexString = hexString.Replace(" """);  
  9.             if ((hexString.Length % 2) != 0)  
  10.                 hexString += " ";  
  11.             byte[] returnBytes = new byte[hexString.Length / 2];  
  12.             for (int i = 0; i < returnBytes.Length; i++)  
  13.                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);  
  14.             return returnBytes;  
  15.         }  

[csharp]  view plain  copy
  1. /// <summary>  
  2.         /// 从汉字转换到16进制  
  3.         /// </summary>  
  4.         /// <param name="s"></param>  
  5.         /// <param name="charset">编码,如"utf-8","gb2312"</param>  
  6.         /// <param name="fenge">是否每字符用逗号分隔</param>  
  7.         /// <returns></returns>  
  8.         public static string ToHex(string s, string charset, bool fenge)  
  9.         {  
  10.             if ((s.Length % 2) != 0)  
  11.             {  
  12.                 s += " ";//空格  
  13.                 //throw new ArgumentException("s is not valid chinese string!");  
  14.             }  
  15.             System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);  
  16.             byte[] bytes = chs.GetBytes(s);  
  17.             string str = "";  
  18.             for (int i = 0; i < bytes.Length; i++)  
  19.             {  
  20.                 str += string.Format("{0:X}", bytes[i]);  
  21.                 if (fenge && (i != bytes.Length - 1))  
  22.                 {  
  23.                     str += string.Format("{0}"",");  
  24.                 }  
  25.             }  
  26.             return str.ToLower();  
  27.         }  

[csharp]  view plain  copy
  1. /// <summary>  
  2.         /// 从16进制转换成汉字  
  3.         /// </summary>  
  4.         /// <param name="hex"></param>  
  5.         /// <param name="charset">编码,如"utf-8","gb2312"</param>  
  6.         /// <returns></returns>  
  7.         public static string UnHex(string hex, string charset)  
  8.         {  
  9.             if (hex == null)  
  10.                 throw new ArgumentNullException("hex");  
  11.             hex = hex.Replace(",""");  
  12.             hex = hex.Replace("\n""");  
  13.             hex = hex.Replace("\\", "");  
  14.             hex = hex.Replace(" """);  
  15.             if (hex.Length % 2 != 0)  
  16.             {  
  17.                 hex += "20";//空格  
  18.             }  
  19.             // 需要将 hex 转换成 byte 数组。   
  20.             byte[] bytes = new byte[hex.Length / 2];  
  21.   
  22.             for (int i = 0; i < bytes.Length; i++)  
  23.             {  
  24.                 try  
  25.                 {  
  26.                     // 每两个字符是一个 byte。   
  27.                     bytes[i] = byte.Parse(hex.Substring(i * 2, 2),  
  28.                     System.Globalization.NumberStyles.HexNumber);  
  29.                 }  
  30.                 catch  
  31.                 {  
  32.                     // Rethrow an exception with custom message.   
  33.                     throw new ArgumentException("hex is not a valid hex number!""hex");  
  34.                 }  
  35.             }  
  36.             System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);  
  37.             return chs.GetString(bytes);  
  38.         }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值