技术宅的结界

 找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 341|回复: 3
收起左侧

【VB6】坑爹的64位整数运算和16进制转换(半成品)

[复制链接]

36

主题

146

帖子

7193

积分

用户组: 管理员

UID
77
精华
11
威望
115 点
宅币
6630 个
贡献
132 次
宅之契约
0 份
在线时间
108 小时
注册时间
2014-2-22
发表于 2019-7-30 04:17:57 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有帐号?立即注册→加入我们

x
废话不说,直接上码。部分64位整数转换为【16进制字符串】的时候存在溢出问题,单纯的计算没问题。
  1. Option Explicit

  2. Private Declare Sub memcpy Lib "kernel32" Alias "RtlMoveMemory" (ByVal dst As Long, ByVal src As Long, ByVal cBytes As Long)

  3. Private Function i64toa(ByVal v As Currency) As String
  4.         Dim hv As Long, lv As Long, i As Long
  5.         Dim ls As String
  6.         If v = 0 Then
  7.                 i64toa = "0": Exit Function
  8.         Else
  9.                 v = v / 10000
  10.         End If
  11.         memcpy VarPtr(hv), VarPtr(v) + 4, 4
  12.         memcpy VarPtr(lv), VarPtr(v) + 0, 4
  13.         ls = Hex$(lv)
  14.         i64toa = Hex$(hv) & String(8 - Len(ls), "0") & ls
  15.         For i = 1 To Len(i64toa)
  16.                 If Left$(i64toa, 1) = "0" Then
  17.                         i64toa = Mid$(i64toa, 2)
  18.                 Else
  19.                         Exit For
  20.                 End If
  21.         Next
  22. End Function

  23. Private Function atoi64(ByVal vs As String) As Currency
  24.         atoi64 = CCur(CDec("&H" & vs))
  25. End Function

  26. Private Sub Command1_Click() '输出64位整数的【十六进制格式字符串】
  27.         On Error Resume Next
  28.         MsgBox i64toa(CCur(CDec("&H7FFFFFFFFFFF"))), , "极大值(64位系统用户模式最高地址)"
  29.         MsgBox i64toa(CCur(CDec("&HFFFF000000000000"))), , "极小值(64位系统内核模式最低地址)"
  30.         MsgBox i64toa(CCur(CDec("&H7FFFFFFFFFFFFFFF"))), , "最大值(溢出)"
  31.         MsgBox i64toa(CCur(CDec("&H8000000000000000"))), , "最小值(溢出)"
  32. End Sub

  33. Private Sub Command2_Click() '【十六进制格式字符串】输出为64位整数
  34.         On Error Resume Next
  35.         MsgBox atoi64("7FFFFFFFFFFF"), , "极大值(64位系统用户模式最高地址)"
  36.         MsgBox atoi64("FFFF000000000000"), , "极小值(64位系统内核模式最低地址)"
  37.         MsgBox atoi64("7FFFFFFFFFFFFFFF"), , "最大值(溢出)"
  38.         MsgBox atoi64("8000000000000000"), , "最小值(溢出)"
  39. End Sub

  40. Private Sub Command3_Click() '【十六进制字符串形式】的64位大整数的加减运算
  41.         Dim a As String: a = "FFFFF80000000000"
  42.         Dim b As String: b = "987654321"
  43.         Dim c As String: c = "7FFF12345678"
  44.         Dim d As String: d = "912345678"
  45.         Dim e As String: e = "FFFFF80012345678"
  46.         Dim f As String: f = "12345678"
  47.         Dim g As String: g = "10000"
  48.         Dim h As String: h = "ABCD"
  49.         MsgBox i64toa(atoi64(a) + atoi64(b))
  50.         MsgBox i64toa(atoi64(c) - atoi64(d))
  51.         MsgBox i64toa(atoi64(e) - atoi64(f))
  52.         MsgBox i64toa(atoi64(g) + atoi64(h))
  53.         MsgBox i64toa(atoi64(g) - atoi64(h))
  54. End Sub
复制代码

评分

参与人数 1威望 +1 宅币 +10 贡献 +1 收起 理由
0xAA55 + 1 + 10 + 1 屌!

查看全部评分

1043

主题

2335

帖子

5万

积分

用户组: 管理员

一只技术宅

UID
1
精华
217
威望
294 点
宅币
18229 个
贡献
36857 次
宅之契约
0 份
在线时间
1738 小时
注册时间
2014-1-26
发表于 2019-7-30 18:36:35 | 显示全部楼层
精华在于CDec、CCur这两个函数的利用

26

主题

111

帖子

1578

积分

用户组: 版主

UID
1821
精华
6
威望
67 点
宅币
1256 个
贡献
47 次
宅之契约
0 份
在线时间
254 小时
注册时间
2016-7-12
发表于 2019-7-30 21:21:20 | 显示全部楼层
坑爹的溢出问题,我的话肯定会用Large_integer

  1. Private Declare Sub memcpy Lib "kernel32" Alias "RtlMoveMemory" (ByVal dst As Long, ByVal src As Long, ByVal cBytes As Long)

  2. Private Type mDec
  3. lo As Long
  4. hi As Long
  5. hh As Long
  6. End Type

  7. Private Type mVar
  8. ty As Integer
  9. tyl As Integer
  10. tt As Long
  11. lo As Long
  12. hi As Long
  13. End Type

  14. Private Function mmi64toa(ByVal sv As Variant) As String
  15. Dim v As Variant: v = sv
  16. Dim ss As String: ss = ""

  17. Dim vDec As mDec
  18. Dim vVar As mVar

  19. memcpy VarPtr(vVar.ty), VarPtr(v), 16
  20. memcpy VarPtr(vDec.lo), VarPtr(vVar.lo), 8
  21. vDec.hh = vVar.tt

  22. If vVar.tyl = &H8000 Then ss = "负数"

  23. mmi64toa = ss & " " & Hex(vDec.hh) & "" & Hex(vDec.hi) & "" & Hex(vDec.lo)
  24. End Function


  25. Private Sub Form_Load()
  26. Dim a As String: a = "79228162514264337593543950335"
  27. Dim b As String: b = "-39614081257132168796771975167"
  28. Dim c As String: c = "9223372036854775807"
  29. Dim d As String: d = "&h1FFFFFFFF"
  30. Dim e As String: e = "1"
  31. Dim f As String: f = "-1"
  32. Dim ss As String

  33. ss = a & " : " & mmi64toa(CDec(a)) & vbCrLf & _
  34. b & " : " & mmi64toa(CDec(b)) & vbCrLf & _
  35. c & " : " & mmi64toa(CDec(c)) & vbCrLf & _
  36. d & " : " & mmi64toa(CDec(d)) & vbCrLf & _
  37. e & " : " & mmi64toa(CDec(e)) & vbCrLf & _
  38. f & " : " & mmi64toa(CDec(f)) & vbCrLf
  39. MsgBox ss
  40. End Sub
复制代码

12

主题

50

帖子

386

积分

用户组: 中·技术宅

UID
2285
精华
0
威望
29 点
宅币
277 个
贡献
1 次
宅之契约
0 份
在线时间
30 小时
注册时间
2017-2-25
发表于 2019-8-1 15:00:42 | 显示全部楼层
哇,这个代码对齐格式,记事本中浏览效果很好啊。。。

本版积分规则

QQ|申请友链||Archiver|手机版|小黑屋|技术宅的结界 ( 滇ICP备16008837号|网站地图

GMT+8, 2019-9-20 11:30 , Processed in 0.102482 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表