Append to byte()

  • Thread starter Thread starter SStory
  • Start date Start date
S

SStory

I need to append to a byte array in a maximum of 256 byte increments.

How??

dim x as fullbytearray()
dim y as windowbytearray()

'y gets it's bytes (256 or less)
need to
x=x & y
or x=x.append(y)

I can't find a way to do it.

Please help,

Shane
 
Hello,

SStory said:
I need to append to a byte array in a maximum of 256
byte increments.

How??

dim x as fullbytearray()
dim y as windowbytearray()

'y gets it's bytes (256 or less)
need to
x=x & y
or x=x.append(y)

What's 'fullbytearray' and 'windowbytearray'? You can use 'ReDim Preserve'
to change the size of an array and use 'Array.CopyTo' to copy the data into
the array. Notice that changing the size of an array is a costly process.
 
SStory said:
I need to append to a byte array in a maximum of 256 byte
increments.

How??

dim x as fullbytearray()
dim y as windowbytearray()

'y gets it's bytes (256 or less)
need to
x=x & y
or x=x.append(y)

I can't find a way to do it.

Have a look at the shared methods System.Array.Copy and the instance methods
System.Array.CopyTo. In general, you can not change the size of an array.
You have to create a new one and copy whatever you want into it.
ReDim Preserve is one way to create a new array and copy the old one in the
new one.
 
Hi SStory,

Did you know that ReDim Preserve is still available?

There's also Array.CopyTo (DestArray, Offset)

ReDim Preserve x (x.Length + y.Length)
y.CopyTo (x, x.Length)

Regards,
Fergus
 
Great. I thought about that. Is preserve the best way or should I use the
array.copy stuff or what?

The idea is append should
create a new byte array
copy old data in first part and then copy data to be appended after then set
the internal buffer to the new one.

Is this right?
 
Hi SStory,

ReDim Preserve is implemented as calls to the .NET Framework proper. This
applies to much of the VB legacy syntax. In practice, unless you're writing
'inner loop' code, the difference in speed is not outweighed, for most
developers, by the ease of developing with something familiar.

Having a C background and therefore being keen on C#, I tend to use the
Framework as much as possible. In part this allows me to universally apply any
new stuff that I learn. It's a very bendy rule, however, especially for
strings. I would probably use ReDim Preserve myself. ReDim Preserve uses
Array.Copy internally.

I gave an incorrect example. The code should have been.

Dim x(4) As byte '5 = 0..4
Dim y(3) As byte '4 = 0..3
: : :
Dim xL As Integer = x.Length
ReDim Preserve x (xL + y.Length - 1) '9 = 0..8
y.CopyTo (x, xL)

I forgot that the value in Dim and ReDim is <not> the number of items but
the upper index. [And I'm sure I'll forget some more times :-)]

This then leads to:
Function ArrayConcat (x() As Byte, y() As Byte) As Byte()
Dim xL As Integer = x.Length
ReDim Preserve x (xL + y.Length - 1)
y.CopyTo (x, xL)
Return x
End Function
or
Function ArrayConcat (x() As Byte, y() As Byte) As Byte()
Dim newx (x.Length + y.Length - 1) As Byte()
x.CopyTo (newx, 0)
y.CopyTo (newx, x.Length)
Return newx
End Function

Regards,
Fergus
 
Back
Top