-
Dec 7th, 2012, 08:58 AM
#1
Thread Starter
Lively Member
[RESOLVED] Mod array
hello i want to add elements in a byte array at the Begin of an array confuse any help
-
Dec 7th, 2012, 09:13 AM
#2
Re: Mod array
try this:
Code:
Dim b1() As Byte = {&H4, &H5, &H6}
Dim b2() As Byte = {&H1, &H2, &H3}
Dim b3() As Byte = b2.Concat(b1).ToArray
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 7th, 2012, 09:29 AM
#3
Re: Mod array
You can't. You'll need to create a new array combining an array of the new elements and the original array, so ...
Dim CombinedArray() As Byte = NewElementsArray.Concat(OriginalByteArray)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 7th, 2012, 01:18 PM
#4
Re: Mod array
You will need to create a new array, as noted, and there are a variety of ways to do that. The older way, making use of Array.Copy, is not quite as easy to read as what dunfiddlin showed, but I tested it out a while back, and Array.Copy is considerably faster than the alternatives. I think it uses some low level memory management code to perform a fast copy under the hood, though I'm not sure about that.
My usual boring signature: Nothing
-
Dec 7th, 2012, 11:51 PM
#5
Thread Starter
Lively Member
-
Dec 8th, 2012, 11:20 AM
#6
Re: Mod array
Originally Posted by Shaggy Hiker
You will need to create a new array, as noted, and there are a variety of ways to do that. The older way, making use of Array.Copy, is not quite as easy to read as what dunfiddlin showed, but I tested it out a while back, and Array.Copy is considerably faster than the alternatives. I think it uses some low level memory management code to perform a fast copy under the hood, though I'm not sure about that.
I'd say it is faster! So much so that if this code was intended for a high use area of the application I would NOT use concat.
Code:
Const alen As Integer = 99999
Dim a1(alen) As Byte
Dim a2(alen) As Byte
'some data
'For x As Integer = 1 To a1.Length - 1
' a1(x) = CByte(x And &HFE) 'note mask
'Next
a1(0) = &HFF 'set start of this array
Array.Copy(a1, a2, a2.Length) 'copy a1 to a2
a2(1) = &HFF 'a2 starts with FF FF
Dim stpw As New Stopwatch
Const tries As Integer = 10
Dim c12() As Byte = New Byte() {}
Dim li As Integer
'use array copies to get combined array
stpw.Restart()
For x As Integer = 1 To tries
Array.Resize(c12, 0) 'set size of destination
Array.Resize(c12, a1.Length + a2.Length) 'set size of destination
Array.Copy(a1, c12, a1.Length) 'copy a1 to combined
Array.Copy(a2, 0, c12, a1.Length, a2.Length) 'copy a2 to combined
Next
stpw.Stop()
Debug.WriteLine(stpw.Elapsed.TotalMilliseconds.ToString("n3"))
li = Array.LastIndexOf(c12, CByte(&HFF)) 'show ffff
Debug.WriteLine("{0}{1} {2}", Convert.ToString(c12(li - 1), 16), Convert.ToString(c12(li), 16), li.ToString("n0"))
Debug.WriteLine("")
Array.Resize(c12, 0) 'set size of destination
stpw.Restart()
For x As Integer = 1 To tries
c12 = a1.Concat(a2).ToArray '<<<<<<<<<<<< using concat
Next
stpw.Stop()
Debug.WriteLine(stpw.Elapsed.TotalMilliseconds.ToString("n3"))
li = Array.LastIndexOf(c12, CByte(&HFF)) 'show ffff
Debug.WriteLine("{0}{1} {2}", Convert.ToString(c12(li - 1), 16), Convert.ToString(c12(li), 16), li.ToString("n0"))
Debug.WriteLine("")
edit: I created a function to combine arrays using Array.Copy, and using it the code was still much faster(50x) than concat.
Last edited by dbasnett; Dec 8th, 2012 at 11:38 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|