你可以将Split输入的字符串分成块Convert每个块到字节,最后将它们具体化ToArray:
// You can let user input the array as a single string
// Test/Demo; in real life it should be
// string source = Console.ReadLine();
string source = "0x10, 0x02, 0x04, 0x00, 0x00, 0x25, 0x23, 0x05, 0xb1, 0x10, 0x03";
byte[] result = source
.Split(new char[] {' ', ':', ',', ';', '\t'}, StringSplitOptions.RemoveEmptyEntries)
.Select(item => Convert.ToByte(item, 16))
.ToArray();
让我们将数组表示为字符串:
string test = string.Join(", ", result
.Select(item => "0x" + item.ToString("x2")));
// "0x10, 0x02, 0x04, 0x00, 0x00, 0x25, 0x23, 0x05, 0xb1, 0x10, 0x03"
Console.Write(test);