gpt4 book ai didi

c# - 向字节数组添加值

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:28 25 4
gpt4 key购买 nike

我从以下两个值开始:

finalString = "38,05,e1,5f,aa,5f,aa,d0";
string[] holder = finalString.Split(',');

我像这样循环通过持有人:

foreach (string item in holder)
{
//concatenate 0x and add the value to a byte array
}

在每次迭代中,我想连接一个 0x 以使其成为一个十六进制值并将其添加到一个字节数组中。

这是我希望字节数组在完成循环后的样子:

byte[] c = new byte[]{0x38,0x05,0xe1,0x5f,0xaa,0x5f,0xaa,0xd0};

到目前为止,我所有的尝试都没有成功。有人可以指出我正确的方向吗?

最佳答案

您不必连接前缀“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);
}

关于c# - 向字节数组添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2929991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com