loword=longvalue and &Hffff hiword=longvalue \ &H10000
Private Sub Command1_Click() Dim x As Long, h As Integer, l As Integer x = &H12345678 h = (x And &HFFFF0000) \ &HFFFF& l = x And &HFFFF& MsgBox h & "," & Hex(h) & vbCrLf & l & "," & Hex(l) End Sub
dim x as long dim hx as integer dim lx as integer hx=x/2^16 lx=hx mod 2^16不知道行不行。
public type word hw as integer lw as integer end typedim s as word,l as long copymemory s,l,4
讨论一下:limitworld(黑色幽灵)的效率较高,但好象有问题吧。long在内存中高低字节应该是反序存放的,因此定义word时应为 public type word lw as integer hw as integer end type 这样结果就正确了。
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
不用转换16进制的。Dim a As Long, b1 As Long, b2 As Long a = 12345678 b1 = a Mod 65536 b2 = a - b1就这么简单
楼上ColdMooon(天上有个太阳)(水中有个月亮)正解,不过写得太简单了,我帮你补全。 BlueBeer(1win) 正解,可读性稍差。 aijie099(茅庐小生)正解,运行效率低,2^16还是直接写出来的好,如果是在循环中可以加速好多。 limitworld(黑色幽灵) 正解,反正也没人规定lw 就一定是作低位,hw一定是高位。 qiqi5521(琪琪) 的答案正解,不过用这么多语句来做这么一件事,实在有点浪费,不合VB的思路。 DemonLoveLizzy(魑魅魍魉)(DavidGuo)(郭大炜)答案和题意不符。我的: dim HL as long dim H as integer dim L as integer L= HL mod 65536 H= HL\65536 或者: dim HL as long dim H as integer dim L as integer L= HL and &HFFFF& H= HL\&HFFFF&我的废话说完了,楼主给分吧,大家可不要拿鸡蛋或西红柿扔我。还是放在一块煮个西红柿蛋汤比较好喝。
唉,大家还是来扔我吧,居然发现自己也写错了: 那个“或者” 应该是: dim HL as long dim H as integer dim L as integer L= HL and &HFFFF& H= HL\&H10000&
这样看来,BlueBeer(1win) 你的也错了。错得和我一样。呵呵
我的答案代码并不多啦,如果只解决这个问题,不需要移位等运算,只取其中两个函数就够了:Option Explicit 'api函数声明, 拷贝内存 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Any, Source As Any, ByVal Length As Long)Public Function HiWord(ByVal DWord As Long) As Integer '取一个双字(DWord)的高位字 'INPUT------------------------------------------- 'DWord 双字 'OUTPUT------------------------------------------ '返回值 DWord参数的高位字 'Last updated by Liu Qi 2004-3-20. Dim intRet As IntegerCopyMemory intRet, ByVal VarPtr(DWord) + 2, 2HiWord = intRetEnd FunctionPublic Function LoWord(ByVal DWord As Long) As Integer '取一个双字(DWord)的低位字 'INPUT------------------------------------------- 'DWord 双字 'OUTPUT------------------------------------------ '返回值 DWord参数的低位字 'Last updated by Liu Qi 2004-3-20. Dim intRet As IntegerCopyMemory intRet, ByVal VarPtr(DWord), 2LoWord = intRetEnd Function
Dim a As Long, b1 As Long, b2 As Long a = 12345678 b1 = a Mod 65536 b2 = a - b1我的为什么错了?b2 = a - b1这才是最简单的
hiword=longvalue \ &H10000
Dim x As Long, h As Integer, l As Integer
x = &H12345678
h = (x And &HFFFF0000) \ &HFFFF&
l = x And &HFFFF&
MsgBox h & "," & Hex(h) & vbCrLf & l & "," & Hex(l)
End Sub
dim hx as integer
dim lx as integer
hx=x/2^16
lx=hx mod 2^16不知道行不行。
hw as integer
lw as integer
end typedim s as word,l as long
copymemory s,l,4
public type word
lw as integer
hw as integer
end type
这样结果就正确了。
超强解决方案
a = 12345678
b1 = a Mod 65536
b2 = a - b1就这么简单
BlueBeer(1win) 正解,可读性稍差。
aijie099(茅庐小生)正解,运行效率低,2^16还是直接写出来的好,如果是在循环中可以加速好多。
limitworld(黑色幽灵) 正解,反正也没人规定lw 就一定是作低位,hw一定是高位。
qiqi5521(琪琪) 的答案正解,不过用这么多语句来做这么一件事,实在有点浪费,不合VB的思路。
DemonLoveLizzy(魑魅魍魉)(DavidGuo)(郭大炜)答案和题意不符。我的:
dim HL as long
dim H as integer
dim L as integer
L= HL mod 65536
H= HL\65536
或者:
dim HL as long
dim H as integer
dim L as integer
L= HL and &HFFFF&
H= HL\&HFFFF&我的废话说完了,楼主给分吧,大家可不要拿鸡蛋或西红柿扔我。还是放在一块煮个西红柿蛋汤比较好喝。
那个“或者”
应该是:
dim HL as long
dim H as integer
dim L as integer
L= HL and &HFFFF&
H= HL\&H10000&
'api函数声明, 拷贝内存
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)Public Function HiWord(ByVal DWord As Long) As Integer
'取一个双字(DWord)的高位字
'INPUT-------------------------------------------
'DWord 双字
'OUTPUT------------------------------------------
'返回值 DWord参数的高位字
'Last updated by Liu Qi 2004-3-20.
Dim intRet As IntegerCopyMemory intRet, ByVal VarPtr(DWord) + 2, 2HiWord = intRetEnd FunctionPublic Function LoWord(ByVal DWord As Long) As Integer
'取一个双字(DWord)的低位字
'INPUT-------------------------------------------
'DWord 双字
'OUTPUT------------------------------------------
'返回值 DWord参数的低位字
'Last updated by Liu Qi 2004-3-20.
Dim intRet As IntegerCopyMemory intRet, ByVal VarPtr(DWord), 2LoWord = intRetEnd Function
a = 12345678
b1 = a Mod 65536
b2 = a - b1我的为什么错了?b2 = a - b1这才是最简单的