Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. inserting value in start of array

inserting value in start of array

Scheduled Pinned Locked Moved Visual Basic
questiondata-structures
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    H3rman
    wrote on last edited by
    #1

    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!!!!

    CPalliniC 1 Reply Last reply
    0
    • H H3rman

      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!!!!

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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.

      In testa che avete, signor di Ceprano?

      H 1 Reply Last reply
      0
      • CPalliniC CPallini

        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.

        H Offline
        H Offline
        H3rman
        wrote on last edited by
        #3

        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.

        CPalliniC 1 Reply Last reply
        0
        • H H3rman

          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.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          It seems that you need a different container, have a look to ArrayList class (that provides the Insert method as well the InsertRange) 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.

          In testa che avete, signor di Ceprano?

          H 1 Reply Last reply
          0
          • CPalliniC CPallini

            It seems that you need a different container, have a look to ArrayList class (that provides the Insert method as well the InsertRange) 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.

            H Offline
            H Offline
            H3rman
            wrote on last edited by
            #5

            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))

            Y 1 Reply Last reply
            0
            • H H3rman

              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))

              Y Offline
              Y Offline
              Yona Low
              wrote on last edited by
              #6

              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 the byteList 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)
              
              H 1 Reply Last reply
              0
              • Y Yona Low

                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 the byteList 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)
                
                H Offline
                H Offline
                H3rman
                wrote on last edited by
                #7

                Thank you guys, I'm forever gratefull.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups