阅读 100

C# BYTE[] 与16进制字符串互相转换

https://www.cnblogs.com/mingjing/p/14473568.html

 

byte[] 转16进制字符串

方法一、

byte[] resultArray = new byte[]{1,2,3,4,5,6,7,8,9};
BitConverter.ToString(resultArray).Replace("-", "")

 

方法二、

 
 
///  
/// 字节数组转16进制字符串 
///  
///  
///  
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
 
 

 

16进制的字符串转为byte[]

 
 
/// 
/// 将16进制的字符串转为byte[]
/// 
/// 
/// 
public static byte[] StrToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
 
 

原文:https://www.cnblogs.com/emanlee/p/15201182.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐