一、Byte 数组转十六进制字符串 /// <summary> /// Byte 数组转十六进制字符串 /// </summa
/// <summary>
/// Byte 数组转十六进制字符串
/// </summary>
/// <param name="Bytes"></param>
/// <returns></returns>
public static string ByteToHex(byte[] Bytes)
{
string str = string.Empty;
foreach (byte Byte in Bytes)
{
str += String.FORMat("{0:X2}", Byte) + " ";
}
return str.Trim();
}
/// <summary>
/// 字符串转十六进制Byte数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[] strToToHexByte(string hexString)
{
try
{
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;
}
catch
{
return null;
}
}
到此这篇关于C#操作Byte数组和十六进制进行互转的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。
--结束END--
本文标题: C#操作Byte数组和十六进制进行互转
本文链接: https://www.lsjlt.com/news/147826.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0