0

If have an array of Bytes in visual basic:

Dim data() As Byte = {0, 128, 0, 4, 9, 9, 32, 0, 0, 0, 0, 0, 0, 0, 0}

Is there a quick and easy way to insert two data values to the front of this array, and knock off the last two values?

Dim data() As Byte = {128, 128, 0, 128, 0, 4, 9, 9, 32, 0, 0, 0, 0, 0, 0}
2
  • 2
    Pretty pointless to use an array if you want to do this. The proper type is a Queue(Of Byte). Commented Sep 2, 2011 at 0:00
  • @Hans, that should be an answer, not a comment. :-) Commented Sep 2, 2011 at 0:14

2 Answers 2

3

Yes. First you need to move all of the existing values up 2 places in your array. Doing so will overwrite the last 2 values. You'll then want to set the first two values of your array.

'Move data up 2 spots.  This needs to be done in reverse order so we don't lose any data
For i as Integer = data.Length - 1 To 2 Step -1
    data(i) = data(i - 2)
End

'Assign the new values
data(0) = 128
data(1) = 128
Sign up to request clarification or add additional context in comments.

Comments

0

You could load the bytes into a vb.net stack with a loop then use the stack.push then rewrite the data back

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.