我正在尝试将字符串数组转换为字节数组。我想使用Encoding.Default.GetBytes()
,但它只接受String
和Char()
。这是代码
Dim aStringArray(2) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim stringArrayBytes() As Byte = Encoding.Default.GetBytes(aStringArray) `this is wrong
答案 0 :(得分:1)
如果您尝试将数组合并为单字节数组,请尝试使用
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))
Next
Dim bytarray() As Byte = stringArrayBytes.ToArray
答案 1 :(得分:1)
根据您的示例,这里采用不同的出价方式:)
Dim aStringArray(2) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim allStrngItems As String = String.Join(String.Empty, aStringArray)
Dim allBytes As Byte() = Encoding.Default.GetBytes(allStrngItems)