VBGood网站全文搜索 Google

首页 - 经验之谈 - 如何转换C代码中的高字和低字
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:8201, 日期:2001-03-10
HiWords and LoWords from Long Values

 When translating C code to VB, you quite often come across the HiWord and LoWord operators, used to pack two integers into a long value. A simple translation of HiWord code will run into difficulties when unsigned integer arithmetic is being
used in the C code and the highest bit of the long value can be set. Since VB doesn注释:t support unsigned arithmetic we have to strip out the high bit and add it back again later to avoid overflows and misleading results.

 Start a new project then add a class module. Add the following code to the module:

Public Property Get LoWord(ByRef lThis As Long) As Long
  LoWord = (lThis And &HFFFF&)
End Property

Public Property Let LoWord(ByRef lThis As Long, ByVal lLoWord As Long)
  lThis = lThis And Not &HFFFF& Or lLoWord
End Property

Public Property Get HiWord(ByRef lThis As Long) As Long
  If (lThis And &H80000000) = &H80000000 Then
   HiWord = ((lThis And &H7FFF0000) \ &H10000) Or &H8000&
   HiWord = (lThis And &HFFFF0000) \ &H10000
  End If
End Property

Public Property Let HiWord(ByRef lThis As Long, ByVal lHiWord As Long)
  If (lHiWord And &H8000&) = &H8000& Then
   lThis = lThis And Not &HFFFF0000 Or ((lHiWord And &H7FFF&) * &H10000) Or &H80000000
   lThis = lThis And Not &HFFFF0000 Or (lHiWord * &H10000)
  End If
End Property


Dim chl As New CHiLo
Dim a As Long
chl.LoWord(a) = &HFFFF0
chl.HiWord(a) = &HFFFF1
Me.Caption = Hex(a)