字节数组转换为字符串c#

Byte array convert to string c#

我有一个字节数组;

bytes = {"0X50","0X04","0X00","0X0A"}

我想像这样将这个数组转换为字符串;

string a = "50-04-00-0A"

我如何在 C# 中修复它? 谢谢

如果您有字节数组(不是所讨论的字符串),您可以使用十六进制 string format(“X”或“x”):

byte[] bytes = new byte[] { 0x50, 0x04, 0x00, 0x0A};
var strings = bytes.Select(b => b.ToString("X"));
Console.WriteLine(string.Join("-", strings)); // prints 50-4-0-A