Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, February 10, 2011 10:45 PM
If I have an array already containing data and I have to alter the byte array after it's been created, how do I remove the last byte of the following array? After the following line of code listed below, I want to change SerialPortBufferTemp to be SerialPortBufferTemp[iBytesRead - 1] or essentiallly remove the last item (a checksum value I want to discard] from the very end of the existing byte array. Thank you.
SerialPortBufferTemp = new byte[iBytesRead];
All replies (5)
Thursday, February 10, 2011 10:54 PM âś…Answered | 1 vote
Unfortunately, you need to copy to a new array in order to do this:
byte[] tmp = new byte[iBytesRead-1];
Array.Copy(SerialPortBufferTemp, tmp, iBytesRead-1);
SerialPortBufferTemp = tmp;
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
Thursday, February 10, 2011 11:04 PM
Thank you Reed. That's it!
Thursday, February 10, 2011 11:13 PM
Could you not call Array.Resize(ref SerialPortBufferTemp, iBytesRead - 1);
Thursday, February 10, 2011 11:29 PM
Could you not call Array.Resize(ref SerialPortBufferTemp, iBytesRead - 1);
Yes. It's basically doing the same thing I did, above (but adding extra checking, since it is more general purpose), but is a bit shorter code wise.Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
Thursday, February 10, 2011 11:35 PM
Sorry Reed, I actually has the reply window open for a good 45 minutes before I actually replied.
I didn't see the responses to this thread.