我想将随机字节数组转换为十六进制,没有错误,但让我显示输出:
第一个输出例如:D2C13E9C4C9378C7
第二个空格应该相同:D2 C1 3 9C 4C 93 78 C7
static void Main(string[] args)
{
// with string interpolation
Random rnd = new Random();
byte[] bytes = new byte[8];
rnd.NextBytes(bytes);
long l = BitConverter.ToInt64(bytes,0);
Console.WriteLine($"Without To Hex Method: {l:X}");
string hexstring = ToHex(bytes);
Console.WriteLine($"With To Hex Method: {hexstring}");
Console.ReadKey();
}
public static string ToHex(byte[] arr)
{
StringBuilder sb = new StringBuilder();
var arr1 = arr.Reverse().ToArray();
for (int i = 0; i < arr1.Length; i++)
{
sb.Append($"{arr1[i]:X2} ");
}
sb.Remove(arr.Length - 1,1);
return sb.ToString();
}