|
楼主 |
发表于 2013-4-17 03:04
|
显示全部楼层
本帖最后由 liucqa 于 2013-4-17 22:59 编辑
优化了一下二楼的算法,发现还是进制转换的方法- Sub test()
- Dim md5$, MD5Val, MD5Val10
- Dim t As Date, i&
- md5 = GetMD5Hash_String("abc")
- t = Timer
- For i = 1 To 500000
- MD5Val = Md5_Numeric(md5)
- Next
- MsgBox Format(Timer - t, "0.00") '8.0
- t = Timer
- For i = 1 To 500000
- MD5Val10 = Convert36_10(md5)
- Next
- MD5Val10 = Convert36_10(md5)
- MsgBox Format(Timer - t, "0.00") '7.6
- End Sub
- Function Md5_Numeric(strMD5 As String)
- Dim i&, j&, b() As Byte
- Dim Numic1, Numic2, step
- Numic1 = CDec(0)
- Numic2 = CDec(0)
- step = CDec(1)
- b = StrConv(Left(strMD5, 32), vbFromUnicode)
- For i = 0 To 15
- If b(i) <= 57 Then
- b(i) = b(i) - 48
- Else
- b(i) = b(i) - 65 + 10
- End If
- Numic1 = Numic1 + step * b(i)
- j = i + 16
- If b(j) <= 57 Then
- b(j) = b(j) - 48
- Else
- b(j) = b(j) - 65 + 10
- End If
- Numic2 = Numic2 + step * b(j)
- step = step * 36 '因MD5字符串都是大写字母,所以b(i)最大是35
- Next
- Md5_Numeric = Numic1 & "," & Numic2
- End Function
- Function Convert36_10(n36 As String)
- Dim basestr As String, b() As Byte
- Dim i&, j&, Numic1, Numic2
- b = StrConv(n36, vbFromUnicode)
- Numic1 = CDec(0)
- Numic2 = CDec(0)
- For i = 0 To 15
- If b(i) <= 57 Then
- b(i) = b(i) - 48
- Else
- b(i) = b(i) - 65 + 10
- End If
- Numic1 = Numic1 * 36 + b(i)
- j = i + 16
- If b(j) <= 57 Then
- b(j) = b(j) - 48
- Else
- b(j) = b(j) - 65 + 10
- End If
- Numic2 = Numic2 * 36 + b(j)
- Next
- Convert36_10 = Numic1 & "," & Numic2
- End Function
复制代码 |
|