gpt4 book ai didi

c# - 用 10 10 替换字节 10

转载 作者:行者123 更新时间:2023-11-30 14:08:53 25 4
gpt4 key购买 nike

您好,我想用 10 x 10 10 替换 byte[]。这是我的代码。如果我的数据为“10 20 10 20 40 50 50 50 50 10 03”,我想将其替换为“10 20 10 10 20 40 50 50 50 50 10 10 03”注意:第一个字节未受影响请按照我的评论,我的想法是将字节数组推到 nxt 位置并再添加 10。

 foreach (var b in command.ToBytes()) 
{

// var c = b;
transmitBuffer[count++] = b; data is formed here
addedbuffer[addall++] = b; duplication is made
}
if (addedbuffer[0] == 16 && addedbuffer[1] == 32 || addedbuffer[50] == 16 && addedbuffer[51] == 03) /
{
/condition on which to enter here
addedbuffer[0] = 0; //making 1st and 2nd value as null
addedbuffer[1] = 0;
for (int i = 0; i < addedbuffer.Length; i++) //till length i will chk
{
if (addedbuffer[i] == 10) //replace 10 by 10 10
addedbuffer[i] = 1010; // error,
}




}

最佳答案

你不能插入数组中(你可以用List<T>来完成),所以看起来你必须创建一个新的 em>数组; Linq 解决方案:

  Byte[] source = new Byte[] {
20, 10, 20, 40, 50, 50, 50, 50, 10, 03
};

var result = source
.SelectMany((item, index) =>
item == 10 && index != 0 ? new Byte[] { item, item } : new Byte[] { item })
.ToArray();

但是,使用 List<Byte> (为了只插入 10)而不是 Byte[]是更好的出路:

  List<Byte> list = List<Byte>() {
20, 10, 20, 40, 50, 50, 50, 50, 10, 03
};

// In order not to read inserted 10's we loop backward
// i >= 1: 1st byte should be preserved as is even if its == 10
for (int i = list.Count - 1; i >= 1; --i)
if (list[i] == 10)
list.Insert(i + 1, 10);

关于c# - 用 10 10 替换字节 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32517802/

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