Results 1 to 4 of 4

Thread: [RESOLVED] Combining byte arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    19

    Resolved [RESOLVED] Combining byte arrays

    Hi Guys,

    I have a quite a few byte arrays that i need to combine.

    Currently, I do:
    Code:
            Dim BytesHeader As Byte() = System.Text.Encoding.UTF8.GetBytes("239 0 " & vbCrLf)
            Dim BytesArticleHeader As Byte() = System.IO.File.ReadAllBytes(ArticleHeader)
            Dim BytesCRLF As Byte() = System.Text.Encoding.UTF8.GetBytes(vbCrLf)
            Dim BytesArticleBody As Byte() = System.IO.File.ReadAllBytes(ArticleBody)
            Dim BytesTerminator As Byte() = System.Text.Encoding.UTF8.GetBytes(vbCrLf & "." & vbCrLf)
    
            Dim Length As Integer = BytesHeader.Length + BytesArticleHeader.Length + BytesCRLF.Length + BytesArticleBody.Length + BytesTerminator.Length
    
            Dim ReturnStr As Byte() = New Byte(Length) {}
            BytesHeader.CopyTo(ReturnStr, 0)
            BytesArticleHeader.CopyTo(ReturnStr, BytesHeader.Length)
            BytesCRLF.CopyTo(ReturnStr, BytesHeader.Length + BytesArticleHeader.Length)
            BytesArticleBody.CopyTo(ReturnStr, BytesHeader.Length + BytesArticleHeader.Length + BytesCRLF.Length)
            BytesTerminator.CopyTo(ReturnStr, BytesHeader.Length + BytesArticleHeader.Length + BytesCRLF.Length + BytesArticleBody.Length)
    
            Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(ReturnStr))  ' as a test
    Is this really the only / best way? It seems really painful to me, especially when the amount of arrays start getting a bit long.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Combining byte arrays

    I expect that Array.CopyTo is as efficient as Array.Copy, in which case, while there are a few other means, there are none that are faster or even close to as fast.
    My usual boring signature: Nothing

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Combining byte arrays

    Why not use List(of Byte) and add them as you go?

    something like this

    Code:
            Dim ReturnStr As New List(Of Byte)
    
            ReturnStr.AddRange(System.Text.Encoding.UTF8.GetBytes("239 0 " & vbCrLf))
            ReturnStr.AddRange(System.IO.File.ReadAllBytes(ArticleHeader))
    'etc
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    19

    Re: Combining byte arrays

    Quote Originally Posted by dbasnett View Post
    Why not use List(of Byte) and add them as you go?

    something like this

    Code:
            Dim ReturnStr As New List(Of Byte)
    
            ReturnStr.AddRange(System.Text.Encoding.UTF8.GetBytes("239 0 " & vbCrLf))
            ReturnStr.AddRange(System.IO.File.ReadAllBytes(ArticleHeader))
    'etc
    Now that makes life alot easier yes It seems from the face of it to do what I need it to do as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width