本文介绍了我在将字节数组数据添加到列表&lt; byte []&gt;时遇到问题缓冲区使用while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个由多个数据消息组成的数据字节流。我正在读取数据并使用状态机将数据分解为单独的消息。每个sepeate消息都存储在字节数组bData [250]中。在继续解码下一个消息之前,我将先前解码的消息放入List< byte []>缓冲区,然后重用字节数组bData来解码下一条消息,然后将其添加到List缓冲区。 我遇到的问题是Buffer.Add()在我的while循环之外工作但不在其中。在循环内部,所有List项目都与最后添加的项目具有相同的数据。 我已经将问题解决为调用一个简单的函数,我可以在其中显示问题,但不幸的是没有解决它!下面是我简单的测试/调试功能: byte [] requestToolDataMsg = new byte [] {0x4B,0x01,0x00,0x00,0xB4}; byte [] requestTest = new byte [] {0x4B,0x16,0x00,0x00,B4}; byte [] clientTableReadMsg = new byte [] {0x4B,0x00,0x27, 0x5A,0x01,0x02,0x16,0x04,0x0A,0x0B,0x0C,0x0D,0x00,0x00,0xA5, 0x5A,0x01,0x02, 0x17,0x05,0x0A,0x0B,0x0C,0x0D,0x0E,0x00,0x00,0xA5, 0x5A,0x01,0x02,0x18,0x06,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x00,0x00, 0xA5, 0xB4}; static 列表< byte [] > buffer = new 列表< byte [] > (); private void UdateList() { byte [] bData = new byte [ 256 ]; int kk = 0 ; buffer.Add(requestToolDataMsg); buffer.Add(clientTableReadMsg); buffer.Clear(); while (kk < 3 ) { kk ++; requestToolDataMsg [ 0 ] =( byte )kk; buffer.Add(requestToolDataMsg); } } 在while循环buffer.Add()之外工作。第一次在while循环中添加正确的数据,但是第二次循环时数据对于第二个项目是正确的,但它也会覆盖第一个项目。在第三次循环之后,所有3个项目都具有第三个项目的值。 即得到: 0x03,0x01,0x00,0x00,0xB4 0x03 ,0x01,0x00,0x00,0xB4 0x03,0x01,0x00,0x00,0xB4 而不是: 0x01,0x01,0x00,0x00,0xB4 0x02,0x01,0x00,0x00,0xB4 0x03,0x01,0x00,0x00,0xB4 我尝试过: 我试图定义List< byte []>函数内部和函数外部的缓冲区,但这没有帮助(在这里抓住吸管。我得到整数列表相同的问题,但是如果我使用字符串数组列表解决方案 I have a stream of data bytes which consists of multiple data messages. I am reading the data in and using a state machine to break the data up into its seperate messages. Each sepeate message is stored in a byte array bData[250]. Before going on to decode the next message I place the previous decoded message into a List<byte[]> buffer, and then reuse the byte array bData to decode the next message and then add it to the List buffer.The problem I am having is that Buffer.Add() works outside of my while loop but not in it. Inside the loop all List items have the same data as the last item added.I have broken the problem down to calling a simple function where I can display the problem, but unfortuneately not solve it! Below is my simple test/debug function:byte[] requestToolDataMsg = new byte[] { 0x4B, 0x01, 0x00, 0x00, 0xB4 };byte[] requestTest = new byte[] { 0x4B, 0x16, 0x00, 0x00, B4 };byte[] clientTableReadMsg = new byte[] { 0x4B, 0x00, 0x27,0x5A, 0x01, 0x02, 0x16, 0x04, 0x0A, 0x0B, 0x0C, 0x0D, 0x00, 0x00, 0xA5,0x5A, 0x01, 0x02, 0x17, 0x05, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x00, 0x00, 0xA5,0x5A, 0x01, 0x02, 0x18, 0x06, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x00, 0xA5,0xB4 };static List<byte[]> buffer = new List<byte[]>();private void UdateList(){byte[] bData = new byte[256];int kk = 0;buffer.Add(requestToolDataMsg);buffer.Add(clientTableReadMsg);buffer.Clear();while (kk < 3){kk++;requestToolDataMsg[0] = (byte)kk;buffer.Add(requestToolDataMsg);}}Outside the while loop buffer.Add() works. The first time inside the while loop the correct data is added, but the second time through the loop the data is correct for the second item but it also overwrites the first item. After the third time through the loop all 3 items have the value of the third item.ie I get:0x03, 0x01, 0x00, 0x00, 0xB40x03, 0x01, 0x00, 0x00, 0xB40x03, 0x01, 0x00, 0x00, 0xB4instead of:0x01, 0x01, 0x00, 0x00, 0xB40x02, 0x01, 0x00, 0x00, 0xB40x03, 0x01, 0x00, 0x00, 0xB4What I have tried:I have tried to define the List<byte[]> buffer inside the function and outside the function, but that does not help (clutching at straws here. I get the same problem with integer lists, but works if I use string array lists 解决方案 这篇关于我在将字节数组数据添加到列表&lt; byte []&gt;时遇到问题缓冲区使用while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 04:39