简体   繁体   中英

Unable to cast object of type 'WhereSelectArrayIterator`2[System.String,System.Byte]' to type 'System.Byte[]'. Vb.net

I'm trying to make a program in vb.net and what it does is that when you open a file it turns the file opened into hexadecimal code, But the problem is that when it saves and tries to convert it back to normal. it results to a: (Unable to cast object of type 'WhereSelectArrayIterator`2[System.String,System.Byte]' to type 'System.Byte[]'.) exception.

Here's the Opening and Saving code

Opening file code: FillWithHex(RichTextBox1,OpenFileDialog1.FileName)

    Async Sub FillWithHex(rtb As RichTextBox, name As String)
    For Each ctl In Controls
        ctl.Enabled = False
    Next ctl
    Dim buff(1000000) As Byte

    Using fs = New FileStream(name, FileMode.Open)
        Using br = New BinaryReader(fs)
            While True
                Dim text = String.Empty
                buff = br.ReadBytes(1000000)
                Await Task.Run(Sub() text = String.Join(" ", buff.
                            Select(Function(b) b.ToString("X2")))).
                            ConfigureAwait(True)
                rtb.AppendText(text)
                If buff.Length < 1000000 Then
                    Exit While
                End If
            End While

        End Using
    End Using
    For Each ctl In Controls
        ctl.Enabled = True
    Next ctl
    ToolStripLabel1.Text = "Status: Idle"
End Sub

And here is the saving code

        Try
        Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16)))
        My.Computer.FileSystem.WriteAllBytes(SaveFileDialog1.FileName, b, False)
    Catch ex1 As Exception
        Try
            Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16)))
            My.Computer.FileSystem.WriteAllBytes(OpenFileDialog1.FileName, b, False)
        Catch ex As Exception
            MsgBox("Exception caught : " + vbNewLine + vbNewLine + ex.ToString, MsgBoxStyle.Critical, "Exception Error")
    End Try
    End Try

Extensions methods from the Enumerable class that you call on object of type IEnumerable(Of T) , like that Select method, generally don't return an array. They generally return some type that implements IEnumerable(Of T) . The specific type generally doesn't matter. If you need an array then you need to call ToArray on that object. ToList will similarly create a List(Of T) . That means you need this:

Dim b = RichTextBox1.Text.
                     Split(" "c).
                     Select(Function(n) Convert.ToByte(n, 16)).
                     ToArray()

Note that I have removed the explicit type declaration, ie As Byte() , and let the type be inferred. That makes for neater code but you don't have to do that if you think that having the explicit type is helpful. Note that I have also removed the useless Convert.ToInt32 call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM