0

我有一个二进制文件“blahblah.dat”,其中包含存储在 16 字节结构中的数据。我文件中的每一行都是 16 个字节长。

我想逐行读取此文件,将二进制数据转换为十进制数据,然后将其显示在文本框中。

我已经搜索过,似乎找不到任何合适的东西。


使用此代码:

Using reader As New BinaryReader(File.Open("C:\Users\User\Desktop\test.dat", FileMode.Open))
        ' Loop through length of file.
        Dim pos As Integer = 0
        Dim length As Integer = reader.BaseStream.Length
        While pos < 1024
            ' Read the integer.
            For i = 0 To 15
                Dim value As Decimal = reader.ReadByte
                ' Write to screen.
                Console.WriteLine(value)
                TextBox1.Text = TextBox1.Text & value & ","
                pos += 1
            Next
            ' Add length of decimal in bytes to position.
            TextBox1.Text = TextBox1.Text & vbCrLf
        End While
    End Using

我得到了这些结果(我认为这是正确的——我稍后会确认):

0,0,128,32,0,0,0,0,2,162,36,18,7,8,3,11,
0,3,128,32,0,0,0,0,1,138,37,18,7,8,3,11,
0,51,128,40,0,0,0,0,0,113,38,18,7,8,3,11,
0,51,128,40,0,0,0,0,3,66,38,18,7,8,3,11,
0,51,128,40,0,0,0,0,2,42,39,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,18,40,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,226,40,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,202,41,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,178,42,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,154,43,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,106,43,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,82,44,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,58,45,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,34,46,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,242,46,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,218,47,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,195,48,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,146,48,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,122,49,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,98,50,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,74,51,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,26,51,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,3,52,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,234,53,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,187,53,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,163,54,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,139,55,18,7,8,3,11,
0,51,130,40,0,0,0,0,0,115,56,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,67,56,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,43,57,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,19,58,18,7,8,3,11,
0,51,130,40,0,0,0,0,3,227,58,18,7,8,3,11,
0,51,130,40,0,0,0,0,2,203,59,18,7,8,3,11,
0,51,130,40,0,0,0,0,1,179,0,19,7,8,3,11,
0,51,130,40,0,0,0,0,0,156,1,19,7,8,3,11,
0,51,130,40,0,0,0,0,3,108,1,19,7,8,3,11,
0,51,130,40,0,0,0,0,2,84,2,19,7,8,3,11,
0,0,128,32,0,0,0,0,3,172,9,19,7,8,3,11,
15,0,128,32,0,0,0,0,1,238,10,19,7,8,3,11,
15,0,128,0,0,0,0,0,0,44,11,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,80,11,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,151,12,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,197,12,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,10,13,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,57,13,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,140,14,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,192,14,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,13,15,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,95,16,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,145,16,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,225,17,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,26,17,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,104,18,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,152,18,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,229,19,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,44,20,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,88,20,19,7,8,3,11,
15,51,128,8,0,0,0,0,0,159,21,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,213,21,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,30,22,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,75,22,19,7,8,3,11,
15,51,128,8,0,0,0,0,1,145,23,19,7,8,3,11,
15,51,128,8,0,0,0,0,3,193,23,19,7,8,3,11,
15,51,128,8,0,0,0,0,2,12,24,19,7,8,3,11,

那是获取数据的最佳方式还是有什么方法可以整理代码?

4

1 回答 1

1

试试这个

Imports System.IO

Module Module1
    Sub Main()
    ' Create the reader in a Using statement.
    ' ... Use File.Open to open the existing binary file.
    Using reader As New BinaryReader(File.Open("blahblah.dat", FileMode.Open))
        ' Loop through length of file.
        Dim pos As Integer = 0
        Dim length As Integer = reader.BaseStream.Length
        While pos < length
        ' Read the integer.
        Dim value As Decimal = reader.ReadDecimal()
        ' Write to screen.
        Console.WriteLine(value)
        ' Add length of decimal in bytes to position.
        pos += 16
        End While
    End Using
    End Sub
End Module
于 2013-06-04T09:40:14.167 回答