VBA位转换器

时间:2017-06-06 17:59:27

标签: vba bitconverter

我想知道如何在Excel VBA中使用bitconverter方法。 我想使用BitConverter.ToInt32转换4个字节,每个字节放在一个32位整数的差值单元格中。

有人可以帮助我在VBA中使用示例吗?我想我正在努力学习语法。

由于

1 个答案:

答案 0 :(得分:2)

  • 使用CopyMemory

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
    
    Public Function BytesToLong(b() As Byte) As Long
      CopyMemory BytesToLong, b(LBound(b)), 4
    End Function
    
  • 没有CopyMemory 1:

    Private Type thebytes
      b(1 To 4) As Byte
    End Type
    
    Private Type thelong
      l As Long
    End Type
    
    
    Public Function BytesToLong(b() As Byte) As Long
      Dim tb As thebytes, tl As thelong
      Dim lb As Long
    
      lb = LBound(b)
      tb.b(1) = b(lb)
      tb.b(2) = b(lb + 1)
      tb.b(3) = b(lb + 2)
      tb.b(4) = b(lb + 3)
    
      LSet tl = tb
    
      BytesToLong = tl.l
    End Function
    
  • 没有CopyMemory 2:

    Public Function BytesToLong(b() As Byte) As Long
      Dim lb As Long
      lb = LBound(b)
    
      If (b(lb + 3) And &H80) = &H80 Then
        BytesToLong = (b(lb) + b(lb + 1) * &H100& + b(lb + 2) * &H10000 + (b(lb + 3) And Not &H80) * &H1000000) Or &H80000000
      Else
        BytesToLong = b(lb) + b(lb + 1) * &H100& + b(lb + 2) * &H10000 + b(lb + 3) * &H1000000
      End If
    
    End Function