PapaBless PapaBless - 16 days ago 8
Vb.net Question

Converting string array to byte array and back

I'm trying to convert a string array to byte array. I would like to use

Encoding.Default.GetBytes()
but it only accepts
String
and
Char()
. This is the code

Dim aStringArray(2) As String

aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"

Dim stringArrayBytes() As Byte = Encoding.Default.GetBytes(aStringArray) `this is wrong

Answer

If you are trying to combine the array into a single byte array try this

    Dim aStringArray(1) As String

    aStringArray(0) = "FileName"
    aStringArray(1) = "FileSize"

    Dim stringArrayBytes As New List(Of Byte)
    For Each s As String In aStringArray
        stringArrayBytes.AddRange(System.Text.Encoding.Default.GetBytes(s).ToList)
    Next

    Dim bytarray() As Byte = stringArrayBytes.ToArray
Comments