求一个大数按位与的算法。。紧急求助

wlaptech 2008-02-03 11:24:07
Dim a As String
Dim b As String
a = "4294967297"
b = "4294967296"
MsgBox a And b
提示溢出。

求一个可以处理大数按位与的算法。
...全文
271 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlaptech 2008-02-11
  • 打赏
  • 举报
回复
感谢jennyvenus 老大...
用户 昵称 2008-02-03
  • 打赏
  • 举报
回复
或这样

Dim a As Long
Dim b As Long
a = &HFFFFFFFF
b = &HFFFFFFFE
MsgBox a And b


用户 昵称 2008-02-03
  • 打赏
  • 举报
回复
拆开分别与,与完了再合起来了。
wlaptech 2008-02-03
  • 打赏
  • 举报
回复
Dim a As Long
Dim b As Long
a = 4294967297
b = 4294967296
MsgBox a And b
用户 昵称 2008-02-03
  • 打赏
  • 举报
回复
Debug.Print hex2dec(myand(dec2hex("429496729742949672974294967297"), dec2hex("429442949672974294967297967296")))

结果
429413171600069656634218710016
用户 昵称 2008-02-03
  • 打赏
  • 举报
回复


Option Explicit

'完整的

Private Sub Command1_Click()
Debug.Print dec2hex("12379814471884843981")

Debug.Print hex2dec("ABCDEFABCDEFABCD")

Debug.Print hex2dec(myand(dec2hex("4294967297"), dec2hex("4294967296")))
End Sub

Private Function myand(ByVal hexa As String, ByVal hexb As String) As String
Dim m As Long
Dim n As Long
Dim a As Byte
Dim b As Byte
Dim c As Byte
Dim r As String

m = Len(hexa)
n = Len(hexb)

If m < n Then
hexa = String(n - m, "0") & hexa
m = n
End If
If n < m Then
hexb = String(m - n, "0") & hexb
n = m
End If

Dim l As Long
l = Len(hexa)

Dim i As Long
For i = 1 To l
a = Val("&h" & Mid(hexa, i, 1))
b = Val("&h" & Mid(hexb, i, 1))
c = a And b

r = r & Trim(Hex$(c))
Next i

myand = r



End Function

Private Function dec2hex(ByVal dec As String) As String

Dim n(1 To 100) As Byte
Dim c As Byte

Dim l As Long
Dim i As Long
Dim j As Long

l = Len(dec)

For i = 1 To l
c = Val(Mid(dec, i, 1))
For j = 100 To 2 Step -1
n(j) = n(j) * 10
Next j
n(100) = n(100) + c

For j = 100 To 2 Step -1
If n(j) > 15 Then
n(j - 1) = n(j - 1) + n(j) \ 16
n(j) = n(j) Mod 16
End If
Next j
Next i

Dim s As String
For i = 1 To 100
If n(i) > 0 Then
For j = i To 100
s = s & Trim(Hex$(n(j)))
Next j
dec2hex = s
Exit Function
End If
Next i
End Function


Private Function hex2dec(ByVal hexx As String) As String

Dim n(1 To 100) As Byte
Dim c As Byte

Dim l As Long
Dim i As Long
Dim j As Long

l = Len(hexx)

For i = 1 To l
c = Val("&h" & Mid(hexx, i, 1))
For j = 100 To 2 Step -1
n(j) = n(j) * 16
Next j
n(100) = n(100) + c

For j = 100 To 2 Step -1
If n(j) > 9 Then
n(j - 1) = n(j - 1) + n(j) \ 10
n(j) = n(j) Mod 10
End If
Next j
Next i

Dim s As String
For i = 1 To 100
If n(i) > 0 Then
For j = i To 100
s = s & Trim(CStr(n(j)))
Next j
hex2dec = s
Exit Function
End If
Next i
End Function





结果 4294967296
用户 昵称 2008-02-03
  • 打赏
  • 举报
回复


Private Function dec2hex(ByVal dec As String) As String

Dim n(1 To 100) As Byte
Dim c As Byte

Dim l As Long
Dim i As Long
Dim j As Long

l = Len(dec)

For i = 1 To l
c = Val(Mid(dec, i, 1))
For j = 100 To 2 Step -1
n(j) = n(j) * 10
Next j
n(100) = n(100) + c

For j = 100 To 2 Step -1
If n(j) > 15 Then
n(j - 1) = n(j - 1) + n(j) \ 16
n(j) = n(j) Mod 16
End If
Next j
Next i

Dim s As String
For i = 1 To 100
If n(i) > 0 Then
For j = i To 100
s = s & Trim(Hex$(n(j)))
Next j
dec2hex = s
Exit Function
End If
Next i
End Function

用户 昵称 2008-02-03
  • 打赏
  • 举报
回复


Option Explicit

Private Sub Command1_Click()
Debug.Print myand("ffffffffffffffffffff", "88776")
End Sub

Private Function myand(ByVal hexa As String, ByVal hexb As String) As String
Dim m As Long
Dim n As Long
Dim a As Byte
Dim b As Byte
Dim c As Byte
Dim r As String

m = Len(hexa)
n = Len(hexb)

If m < n Then
hexa = String(n - m, "0") & hexa
m = n
End If
If n < m Then
hexb = String(m - n, "0") & hexb
n = m
End If

Dim l As Long
l = Len(hexa)

Dim i As Long
For i = 1 To l
a = Val("&h" & Mid(hexa, i, 1))
b = Val("&h" & Mid(hexb, i, 1))
c = a And b

r = r & Trim(Hex$(c))
Next i

myand = r



End Function

wlaptech 2008-02-03
  • 打赏
  • 举报
回复
里面没有求按位与的函数阿
cnzdgs 2008-02-03
  • 打赏
  • 举报
回复
如果是数值相与就先把字符串转成无符号整数,进行与运算后再转成字符串;
如果是字符串相与就把字符串拆成数组,进行与运算后再合成字符串。
用户 昵称 2008-02-03
  • 打赏
  • 举报
回复
我以前算这个的时候都用vc 写个dll,因为vc有64位长整数。

这里有一个代码,vb的,看看吧。

http://www.vbgood.com/viewthread.php?tid=26111

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧