-5

我想知道如何在 Excel VBA 中使用 bitconverter 方法。我想使用 BitConverter.ToInt32 将 4 个字节转换为 32 位整数的不同单元格中的每个字节。

有人可以帮我举一个在 VBA 中使用的例子吗?我想我在语法上苦苦挣扎。

谢谢

4

1 回答 1

3
  • 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
    
  • 没有CopyMemory1:

    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
    
  • 没有CopyMemory2:

    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
    
于 2017-06-06T18:31:28.530 回答