Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA字节值正确写入二进制文件,但在读回userform时显示不同_Excel_Vba_Binary_Endianness - Fatal编程技术网

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

Excel VBA字节值正确写入二进制文件,但在读回userform时显示不同,excel,vba,binary,endianness,Excel,Vba,Binary,Endianness,我有一个二进制文件,其中包含了big-endian格式的2字节和4字节整数的值。我可以读取文件并在用户窗体上显示值 Public Sub Binary_Header_To_Array() Application.StatusBar = "Reading Seg-Y File " & file_count & " of " & UBound(segyInFiles) Call SetBinaryHeaderByteFormat Open segy

我有一个二进制文件,其中包含了big-endian格式的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,它们将以little-endian格式写入文件。由于我希望它们以big-endian的形式存储,所以我将每个值分隔为单独的字节,然后以相反的顺序将字节写入文件。在每个步骤中,我导出一个ascii文件,其中包含文件中的字节索引和相应的字节值

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

当我将新创建的文件读回并显示在userform上时,问题就出现了。2字节(整数)值显示正确,但4字节(长)值显示的数字不同。我创建的检查文件显示字节顺序不正确

例如,在原始文件中,值13029作为4字节有符号整数存储在位置3201-3204处,各个字节值为:

Byte 1 = 0 (3201)
Byte 2 = 0 (3202)
Byte 3 = 50 (3203)
Byte 4 = 229 (3204)
从小端到大端转换以写回文件时,值写入为:

Byte 4 = 0 (3201)
Byte 3 = 0 (3202)
Byte 2 = 50 (3203)
Byte 1 = 229 (3204)
在使用“put”语句之前,我检查了这个值,然后立即使用“get”语句返回值并打印到ascii文件。顺序同上

但是,当我打开文件读回userform时,字节的值为:

Byte 1 = 50 (3201)
Byte 2 = 229 (3202)
Byte 3 = 229 (3203)
Byte 4 = 0 (3204)
索引是正确的,例如3201到3204的4字节值,只是字节值混淆了

有人能解释为什么会发生这种情况,或者我能做些什么进一步的检查来缩小问题的范围吗。任何帮助都将不胜感激

谢谢


Rob

重新检查了我的代码,在所有初始检查和写入文件后,发现一些旧的错误代码仍然存在。注释掉了这一点,它工作得非常完美。

如果没有读取和写入文件的IO代码,这可能无法回答。这是您可以编辑到问题中的内容吗?谢谢,我现在不在电脑中,但今晚晚些时候将添加代码。在编辑问题时,我必须通过代码发布相关内容并整理评论。无论如何,我发现我的第一次尝试(转换为十六进制值)没有被注释掉。所以所有的代码都被纠正了,所有的检查都是正确的,然后就在最后一分钟,它覆盖了正确的值,就在End Sub之前。我把这个错误的代码注释掉了,它工作得很好。