Memory 如何在块中读取字节以避免内存不足异常?还是别的什么

Memory 如何在块中读取字节以避免内存不足异常?还是别的什么,memory,byte,out,Memory,Byte,Out,经过两天的搜索和尝试,我终于决定在这里发帖寻求帮助 我有一个代码可以读取和写入文件的字节(如十六进制编辑器),从这里可以得到非常好的效果: 代码如下: Public Class Form1 Private Shared Function HexStringToByteArray(ByRef strInput As String) As Byte() Dim length As Integer Dim bOutput As Byte() Di

经过两天的搜索和尝试,我终于决定在这里发帖寻求帮助

我有一个代码可以读取和写入文件的字节(如十六进制编辑器),从这里可以得到非常好的效果: 代码如下:

Public Class Form1
    Private Shared Function HexStringToByteArray(ByRef strInput As String)
As Byte()
        Dim length As Integer
        Dim bOutput As Byte()
        Dim c(1) As Integer
        length = strInput.Length / 2
        ReDim bOutput(length - 1)
        For i As Integer = 0 To (length - 1)
            For j As Integer = 0 To 1
                c(j) = Asc(strInput.Chars(i * 2 + j))
                If ((c(j) >= Asc("0")) And (c(j) <= Asc("9"))) Then
                    c(j) = c(j) - Asc("0")
                ElseIf ((c(j) >= Asc("A")) And (c(j) <= Asc("F"))) Then
                    c(j) = c(j) - Asc("A") + &HA
                ElseIf ((c(j) >= Asc("a")) And (c(j) <= Asc("f"))) Then
                    c(j) = c(j) - Asc("a") + &HA
                End If
            Next j
            bOutput(i) = (c(0) * &H10 + c(1))
        Next i
        Return (bOutput)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim myFile As String = "..\..\Test.jpg"
        Dim myBytes As Byte() =
My.Computer.FileSystem.ReadAllBytes(myFile)
        Dim txtTemp As New System.Text.StringBuilder()
        For Each myByte As Byte In myBytes
            txtTemp.Append(myByte.ToString("X2"))
        Next
        RichTextBox1.Text = txtTemp.ToString()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        Dim myFile As String = "..\..\Test2.jpg"
        Dim myBytes As Byte() = HexStringToByteArray(RichTextBox1.Text)
        My.Computer.FileSystem.WriteAllBytes(myFile, myBytes, False)
    End Sub
End Class
公共类表单1
私有共享函数HexStringToByteArray(ByRef strInput作为字符串)
作为字节()
将长度设置为整数
作为字节()的Dim bOutput
作为整数的Dim c(1)
长度=条纹长度/2
ReDim bOutput(长度-1)
对于i,整数=0到(长度-1)
对于j,作为整数=0到1
c(j)=Asc(严格字符(i*2+j))
如果((c(j)>=Asc(“0”)和(c(j)=Asc(“A”))和(c(j)=Asc(“A”)和(c(j)