我从下面的两个值开始:
finalString = "38,05,e1,5f,aa,5f,aa,d0";
string[] holder = finalString.Split(',');
Run Code Online (Sandbox Code Playgroud)
我像这样循环通过持有者:
foreach (string item in holder)
{
//concatenate 0x and add the value to a byte array
}
Run Code Online (Sandbox Code Playgroud)
在每次迭代中,我想连接一个0x以使其成为十六进制值并将其添加到字节数组中.
这就是我在完成循环时希望字节数组的样子:
byte[] c = new byte[]{0x38,0x05,0xe1,0x5f,0xaa,0x5f,0xaa,0xd0};
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的所有尝试都没有成功.有人能指出我正确的方向吗?
您不必连接前缀"0x".解析文字时,C#编译器使用此前缀,但byte.Parse不使用它
byte[] myByteArray = new byte[holder.Length];
int i = 0;
foreach (string item in holder)
{
myByteArray[i++] = byte.Parse(item, System.Globalization.NumberStyles.AllowHexSpecifier);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17544 次 |
| 最近记录: |