Converting big-endian into little-endian and vice-versa in VBA

后端 未结 3 1099
借酒劲吻你
借酒劲吻你 2021-01-07 01:34

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

3条回答
  •  既然无缘
    2021-01-07 02:38

    Here is a simple ASCII conversion:

    Public Function SwapLong(Data As Long) As Long
    
        Const Sz As Integer = 3
        Dim Bytes(Sz) As Byte
        Dim n As Integer
        Dim HexStr As String
        Dim SwpStr As String
    
        HexStr = Right("00000000" + Hex(Data), 2 * (Sz + 1))
        SwpStr = vbNullString
    
        For n = 0 To Sz
            SwpStr = SwpStr + Mid(HexStr, (Sz - n) * 2 + 1, 2)
        Next n
    
        SwapLong = CLng("&h" + SwpStr)
    
    End Function
    
    Public Function SwapInt(Data As Integer) As Integer
    
        Const Sz As Integer = 1
        Dim Bytes(Sz) As Byte
        Dim n As Integer
        Dim HexStr As String
        Dim SwpStr As String
    
        HexStr = Right("0000" + Hex(Data), 2 * (Sz + 1))
        SwpStr = vbNullString
    
        For n = 0 To Sz
            SwpStr = SwpStr + Mid(HexStr, (Sz - n) * 2 + 1, 2)
        Next n
    
        SwapInt = CInt("&h" + SwpStr)
    
    End Function
    

提交回复
热议问题