By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,703 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,703 IT Pros & Developers. It's quick & easy.

Append to byte()

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hello,

"SStory" <Th*******@TAKETHISSPAMBUSTEROUT.Softhome.net> schrieb:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #2

P: n/a
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
Nov 20 '05 #3

P: n/a
"SStory" <Th*******@TAKETHISSPAMBUSTEROUT.Softhome.net> schrieb
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.
--
Armin

Nov 20 '05 #4

P: n/a
SStory,
In addition to the others comments I posted the start of a ByteBuffer class
that functions very similar to the System.Text.StringBuffer class in that it
maintains a byte array internally allowing you to append byte arrays on the
end, expanding the internal array as needed.

http://groups.google.com/groups?q=By...phx.gbl&rnum=3

Hope this helps
Jay

"SStory" <Th*******@TAKETHISSPAMBUSTEROUT.Softhome.net> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #5

P: n/a
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?
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
SStory,
In addition to the others comments I posted the start of a ByteBuffer class that functions very similar to the System.Text.StringBuffer class in that it maintains a byte array internally allowing you to append byte arrays on the end, expanding the internal array as needed.

http://groups.google.com/groups?q=By...phx.gbl&rnum=3
Hope this helps
Jay

"SStory" <Th*******@TAKETHISSPAMBUSTEROUT.Softhome.net> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
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


Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.