当前位置:编程学习 > VB >>

如何转换C代码中的高字和低字

 

 

   当把C代码翻译到VB时,你经常会遇到高字和低字的操作,通常是把两个整型值合成一个长整型。当在C代码中使用了无符号整型,这最高一位是可以被设置的,因此简单的直接转换高字将会遇到困难。由于VB不支持无符号算术符,我们只好剥去这最高一位,在稍后再把它加回来,以避免溢出和错误的结果。

 

开始一个新工程,增添一个标准模块。把以下代码写入模块:

Start a new project then add a 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&

  Else

   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

  Else

   lThis = lThis And Not &HFFFF0000 Or (lHiWord * &H10000)

  End If

End Property 

补充:软件开发 , Vb ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,