-
Jul 31st, 2014, 06:24 PM
#1
Thread Starter
Junior Member
[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.
-
Jul 31st, 2014, 06:26 PM
#2
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
-
Jul 31st, 2014, 08:03 PM
#3
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
-
Aug 1st, 2014, 03:08 AM
#4
Thread Starter
Junior Member
Re: Combining byte arrays
Originally Posted by dbasnett
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|