1 回答
TA贡献1825条经验 获得超6个赞
检查一下,我修改了您的代码,您需要的字节数组是 byteArray
顺便说一句,您的循环条件是错误的,应该是 i < valueArray.Length
我不确定这是不是你想要的
using System;
using System.Collections.Generic;
using System.Globalization;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.Write("Input bytes: ");
String input = Console.ReadLine();
String[] valueArray = input.Split(new string[] { "\\x" }, StringSplitOptions.RemoveEmptyEntries);
List<byte> byteArray = new List<byte>();
for (int i = 0; i < valueArray.Length; i++) {
int ret = -1;
string hex = valueArray[i];
if (hex.StartsWith("0x"))
hex = hex.Substring(2, hex.Length - 2);
if (!Int32.TryParse(hex, NumberStyles.HexNumber, null, out ret) || ret < 0 || ret > 0xff) {
Console.WriteLine("{0} is not valid hex", valueArray[i]);
return;
}
byteArray.Add((byte)ret);
}
}
}
}
- 1 回答
- 0 关注
- 185 浏览
添加回答
举报