byte数组和其他数据类型之间的转化

byte数组和其他数据类型之间的转化

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()
    Dim test(0 To 3) As Byte
    Dim i As Long, j As Long
    i = 1000
    '从long转化为byte数组
     CopyMemory test(0), i, 4
     '从byte数组转化为long
     CopyMemory j, test(0), 4
     MsgBox j
End Sub

 

或者根据下面的代码中的函数进行转换:
Public Function HiByte(ByVal wParam As Integer) As Byte
  'note: VB4-32 users should declare this function As Integer
   HiByte = (wParam And &HFF00&) / (&H100)
End Function

Public Function LoByte(ByVal wParam As Integer) As Byte
  'note: VB4-32 users should declare this function As Integer
   LoByte = wParam And &HFF&
End Function

Public Function HiWord(wParam As Long) As Integer
   If wParam And &H80000000 Then
      HiWord = (wParam / 65535) - 1
   Else
      HiWord = wParam / 65535
   End If
End Function

Public Function LoWord(wParam As Long) As Integer
   If wParam And &H8000& Then
      LoWord = &H8000& Or (wParam And &H7FFF&)
   Else
      LoWord = wParam And &HFFFF&
   End If
End Function

Public Function LoWordCM(wParam As Long) As Integer
  'using API
   CopyMemory LoWordCM, wParam, 2
End Function

Public Function LShiftWord(ByVal w As Integer, ByVal c As Integer) As Integer
   Dim dw As Long
   dw = w * (2 ^ c)
   If dw And &H8000& Then
      LShiftWord = CInt(dw And &H7FFF&) Or &H8000&
   Else
      LShiftWord = dw And &HFFFF&
   End If
End Function

Public Function RShiftWord(ByVal w As Integer, ByVal c As Integer) As Integer
   Dim dw As Long
   If c = 0 Then
      RShiftWord = w
   Else
      dw = w And &HFFFF&
      dw = dw / (2 ^ c)
      RShiftWord = dw And &HFFFF&
   End If
End Function
'两个byte转换为一个integer
Public Function MakeWord(ByVal bHi As Byte, ByVal bLo As Byte) As Integer
   If bHi And &H80 Then
      MakeWord = (((bHi And &H7F) * 256) + bLo) Or &H8000&
   Else
      MakeWord = (bHi * 256) + bLo
   End If
End Function
'两个integer转换为一个long
Public Function MakeDWord(wHi As Integer, wLo As Integer) As Long
   If wHi And &H8000& Then
      MakeDWord = (((wHi And &H7FFF&) * 65536) Or _
                    (wLo And &HFFFF&)) Or &H80000000
   Else
      MakeDWord = (wHi * 65535) + wLo
   End If
End Function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值