在VBA中将big-endian转换为little-endian,反之亦然 [英] Converting big-endian into little-endian and vice-versa in VBA
问题描述
我的机器是低位字节序(Intel字节顺序).我需要读取一个以Motorola/IEEE字节顺序("big-endian")包含16位带符号整数数据的二进制文件,然后进行一些计算,最后将生成的integer
数据写入big-endian二进制文件中.
My machine is little-endian (Intel byte order). I need to read a binary file containing 16-bit signed integer data in Motorola/IEEE byte order ("big-endian"), then do some calculations, and finally write the resulting integer
data in a big-endian binary file.
我如何在VBA中执行上述操作,即将大端转换为小端,反之亦然?
How do I do the above in VBA, i.e. convert big-endian into little-endian and vice-versa?
原因是,我正在处理NASA 航天飞机雷达地形任务数据( HGT文件格式).
The reason is, I'm processing NASA Shuttle Radar Topography Mission data (HGT file format).
推荐答案
下面是一个子例程,可以帮助您入门:
Here is a subroutine that may get you started:
Public Sub ProcessData()
Dim inputFileName As String
Dim outputFileName As String
Dim wordCount As Integer
Dim i As Integer
Dim msb As Byte
Dim lsb As Byte
Dim unsignedWord As Long
Dim word As Integer
inputFileName = "C:\Input.bin"
outputFileName = "C:\Output.bin"
wordCount = FileLen(inputFileName) / 2
Open inputFileName For Binary Access Read As #1
Open outputFileName For Binary Access Write As #2
For i = 1 To wordCount
Get #1, , msb
Get #1, , lsb
unsignedWord = CLng(msb) * 256 + lsb
If unsignedWord > 32768 Then
word = -CInt(65536 - unsignedWord)
Else
word = CInt(unsignedWord)
End If
' Do something with each word.
word = -word
If word < 0 Then
unsignedWord = 65536 + word
Else
unsignedWord = word
End If
msb = CByte(unsignedWord / 256)
lsb = CByte(unsignedWord Mod 256)
Put #2, , msb
Put #2, , lsb
Next
Close #1
Close #2
End Sub
这篇关于在VBA中将big-endian转换为little-endian,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!