inserting value in start of array
-
I have struggling with this for a few days now and i can't come up with a solution, damn it I can't sleep. I have a array of bytes (iv'e used the UTF8 encoding from string to bytes) but i wan't to insert 4 bytes (255 = &HFF) before the result from the encoding. How do i do this the easiest way? do i have to loop through all values in the array? that seems to be a huge waste of cpu. Thanks. And a merry merry X-Mas!!!!
-
I have struggling with this for a few days now and i can't come up with a solution, damn it I can't sleep. I have a array of bytes (iv'e used the UTF8 encoding from string to bytes) but i wan't to insert 4 bytes (255 = &HFF) before the result from the encoding. How do i do this the easiest way? do i have to loop through all values in the array? that seems to be a huge waste of cpu. Thanks. And a merry merry X-Mas!!!!
-
If you post the code maybe it will help us.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Imports System.Text Dim myBytes() As Byte myBytes = Encoding.UTF8.GetBytes("TESTSTRING") Dim myStartBytes() As Byte = {255, 255, 255, 255} I want the mystartbytes to be in mybytes(0,1,2,3) but not overwrite but to push in the value so the rest of the array is moved 4 steps. Do you understand what i mean?... My brain has freezed up.... thanks for your help.
-
Imports System.Text Dim myBytes() As Byte myBytes = Encoding.UTF8.GetBytes("TESTSTRING") Dim myStartBytes() As Byte = {255, 255, 255, 255} I want the mystartbytes to be in mybytes(0,1,2,3) but not overwrite but to push in the value so the rest of the array is moved 4 steps. Do you understand what i mean?... My brain has freezed up.... thanks for your help.
It seems that you need a different container, have a look to
ArrayList
class (that provides theInsert
method as well theInsertRange
) on MSDN. Hope that helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
It seems that you need a different container, have a look to
ArrayList
class (that provides theInsert
method as well theInsertRange
) on MSDN. Hope that helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Hi again.... I got it workin but I'm note quite happy with my coding.... feels like alot of unnecessary steps just for combinding two arrays with a few bytes in it. Dim myMessage As New ArrayList Dim myByte() As Byte = {255, 255, 255, 255} myMessage.AddRange(myByte) myMessage.AddRange(Encoding.UTF8.GetBytes("rcon popeye status")) myByte = myMessage.ToArray(GetType(Byte))
-
Hi again.... I got it workin but I'm note quite happy with my coding.... feels like alot of unnecessary steps just for combinding two arrays with a few bytes in it. Dim myMessage As New ArrayList Dim myByte() As Byte = {255, 255, 255, 255} myMessage.AddRange(myByte) myMessage.AddRange(Encoding.UTF8.GetBytes("rcon popeye status")) myByte = myMessage.ToArray(GetType(Byte))
In .NET 2.0 this is 2 lines
Dim byteList As New List(Of Byte)(New Byte() {255, 255, 255, 255}) byteList.AddRange(UTF8.GetBytes("TESTSTRING"))
Since in .NET 2.0 the List is Generic, you can use the
ToArray
Methohd to get a byte array of thebyteList
on the fly Example:Dim pass() as byte = byteList.ToArray
Or even in method calls:
Dim writer As New BinaryWriter(New FileStream("C:\test.txt", FileMode.CreateNew)) writer.Write(byteList.ToArray)
-
In .NET 2.0 this is 2 lines
Dim byteList As New List(Of Byte)(New Byte() {255, 255, 255, 255}) byteList.AddRange(UTF8.GetBytes("TESTSTRING"))
Since in .NET 2.0 the List is Generic, you can use the
ToArray
Methohd to get a byte array of thebyteList
on the fly Example:Dim pass() as byte = byteList.ToArray
Or even in method calls:
Dim writer As New BinaryWriter(New FileStream("C:\test.txt", FileMode.CreateNew)) writer.Write(byteList.ToArray)