首页 文章

VBA字节值正确写入二进制文件,但在读回userform时显示不同

提问于
浏览
0

我有一个二进制文件,包含大端格式的2字节和4字节整数值 . 我可以读取文件并在用户表单上显示值 .

Public Sub Binary_Header_To_Array()
    Application.StatusBar = "Reading Seg-Y File " & file_count & " of " & UBound(segyInFiles)

    Call SetBinaryHeaderByteFormat

    Open segyInFiles(CurrentFile) For Binary As #1

    z = 1

    For byte_count = 1 To UBound(BinaryHeader_byte_index)                'byte_count = number of fields in Seg-Y header
                    'Set pointer to byte index position
                    Seek #1, BinaryHeader_byte_index(byte_count)        'byte index is an array containing the number of fields in the Seg-Y header

                    Select Case BinaryHeader_byte_formats(byte_count)

                        Case 1 '16 bit integer (two's complement) / INTEGER
                            Call Byte_Formats.sixteen_bit_signed_integer
                            BinaryArray(byte_count, z) = integer_value 'CStr(integer_value)

                        Case 2 '32 bit integer (two's complement) / LONG
                            Call Byte_Formats.thirty_two_bit_signed_integer
                            BinaryArray(byte_count, z) = long_value 'CStr(long_value)

                    End Select

                Next byte_count

    Close #1
 End Sub

'**************************************************************

 Public Sub Binary_Header_To_UserForm()

    For i = 1 To UBound(BinaryHeader_byte_index)
        formMAIN.Controls("TextBox_BinHead_" & i).Text = BinaryArray(i, z)
    Next i

    formMAIN.TextBox_BinHead_35.Text = BinaryArray(35, 1) / 256

 End Sub

'**************************************************************

'Case 1 '16 bit integer (two's complement) / INTEGER
Public Sub sixteen_bit_signed_integer()
    Get #1, , read_two_bytes
    integer_value = (-Int(read_two_bytes(1) / 128) * 128 + (read_two_bytes(1) Mod 128)) * 256 + read_two_bytes(2)
    'out_string = out_string & Space(column_width - Len(CStr(integer_value))) & CStr(integer_value)
End Sub

'**************************************************************

'Case 2 '32 bit integer (two's complement) / LONG
Public Sub thirty_two_bit_signed_integer()
    Get #1, , read_four_bytes
    long_value = (-Int(read_four_bytes(1) / 128) * 128 + (read_four_bytes(1) Mod 128)) * 2 ^ 24 + read_four_bytes(2) * 2 ^ 16 _
    + read_four_bytes(3) * 2 ^ 8 + read_four_bytes(4)

    txtOutFileinput = OutFileDir & SegyFilename & "_InputBytes.txt"

' This prints the byte values to an ascii file as they are read
Open txtOutFileinput For Append As #6

    Print #6, long_value
    Print #6, "Byte1_" & read_four_bytes(1) & "_" & Seek(1)
    Print #6, "Byte2_" & read_four_bytes(2) & "_" & Seek(1)
    Print #6, "Byte3_" & read_four_bytes(3) & "_" & Seek(1)
    Print #6, "Byte4_" & read_four_bytes(4) & "_" & Seek(1)

    Close #6

    'out_string = out_string & Space(column_width - Len(CStr(integer_value))) & CStr(integer_value)
End Sub

然后,我可以编辑用户表单上的值并写回新的二进制文件(原始文件的副本) . 这些值在VBA中被定义为Ineteger和Long vairables,它们将以小端格式写入文件 . 因为我希望它们存储为大端,我将每个值分成单个字节,然后我以相反的顺序将字节写入文件 . 在每个步骤中,我导出一个ascii文件,其中包含文件中的字节索引和相应的字节值 .

这一切都检查出字节值是否正在写入文件中的正确位置,正如我对Big Endian所期望的那样 .

当我将新创建的文件读回到userform上显示时,会出现问题 . 正确显示2字节(整数)值,但4字节(长整数)值显示不同的数字 . 我创建的检查文件显示字节的排序不正确 .

作为原始文件中的示例,值13029被存储为位置3201-3204处的4字节有符号整数,各个字节值是;

Byte 1 = 0 (3201)
Byte 2 = 0 (3202)
Byte 3 = 50 (3203)
Byte 4 = 229 (3204)

从Little Endian转换为Big Endian以写回文件时,该值写为:

Byte 4 = 0 (3201)
Byte 3 = 0 (3202)
Byte 2 = 50 (3203)
Byte 1 = 229 (3204)

我在使用'put'语句之前检查了这个,然后立即再次使用'get'语句返回值并打印到ascii文件 . 订单与上述相同 .

但是,当我打开文件以读回用户窗体时,字节具有以下值:

Byte 1 = 50 (3201)
Byte 2 = 229 (3202)
Byte 3 = 229 (3203)
Byte 4 = 0 (3204)

索引是正确的,例如在3201到3204处的4字节值,只是字节值已被混淆 .

任何人都可以帮助解释为什么会发生这种情况或者我可以采取哪些进一步的检查来缩小问题发生的位置 . 任何帮助将不胜感激 .

谢谢

1 回答

  • 0

    重新检查我的代码,发现一些旧的错误代码仍然存在,在所有初始检查和写入文件之后 . 评论出来,它完美无缺 .

相关问题