“HEX 而不是 ASCII”是什么意思?HEX 字符串通常是仅由 [0-9A-F] 字符组成的 ASCII 编码字符串。
这是一个 C# 扩展方法,它将字节数组转换为十六进制数字对的字符串,表示原始字节数组的字节:
using System;
using System.Linq;
using System.Text;
namespace Substitute.With.Your.Project.Namespace.Extensions
{
static public class ByteUtil
{
/// <summary>
/// byte[] to hex string
/// </summary>
/// <param name="self"></param>
/// <returns>a string of hex digit pairs representing this byte[]</returns>
static public string ToHexString(this byte[] self)
{
return self
.AsEnumerable()
.Aggregate(
new StringBuilder(),
(sb, value)
=> sb.Append(
string.Format("{0:X2}", value)
)
).ToString();
}
}
}
然后在其他地方你只是use
扩展
using Substitute.With.Your.Project.Namespace.Extensions;
并这样称呼它
aByteArray.ToHexString();
(代码未经测试;YMMW)