分享

进制转换的代码

 zele 2011-02-12
  1. '10 to 16
  2. Public Function Dec2Hex$(ByVal lngS&)
  3.     Dim t$
  4.     t = Hex(lngS)
  5.     If (Len(t) And 1) = 1 Then t = "0" & t
  6.     Dec2Hex = t
  7. End Function

  8. '16 to 2
  9. Public Function Hex2Bin$(ByVal strHex$)
  10.     Const hS As String = "0124937FEDA5B6C8"
  11.     Const bhS As String = "0000100111101011000"
  12.     Dim i%
  13.     For i = 1 To Len(strHex)
  14.         Hex2Bin = Hex2Bin & Mid$(bhS, InStr(1, hS, Mid$(strHex, i, 1), vbTextCompare), 4)
  15.     Next
  16. End Function

  17. '10 to 2
  18. Public Function Dec2Bin$(ByVal lngS&)
  19.     Dec2Bin = Hex2Bin(Dec2Hex(lngS))
  20. End Function

  21. '10 to 8
  22. Public Function Dec2Oct$(ByVal lngS&)
  23.     Dec2Oct = Oct(lngS)
  24. End Function

  25. '8 to 2
  26. Public Function Oct2Bin$(ByVal strOct$)
  27.     Const oS As String = "01253764"
  28.     Const boS As String = "0001011100"
  29.     Dim i%
  30.     For i = 1 To Len(strOct)
  31.         Oct2Bin = Oct2Bin & Mid$(boS, InStr(1, oS, Mid$(strOct, i, 1), vbTextCompare), 3)
  32.     Next
  33. End Function

  34. '2 to 8
  35. Public Function Bin2Oct$(ByVal strBin$)
  36.     Const oS As String = "01253764"
  37.     Const boS As String = "0001011100"
  38.     Dim i%, t$
  39.     strBin = Right$("00" & strBin, ((Len(strBin) + 2) \ 3) * 3)
  40.     For i = 1 To Len(strBin) Step 3
  41.         t = t & Mid$(oS, InStr(1, boS, Mid$(strBin, i, 4), vbTextCompare), 1)
  42.     Next
  43.     Bin2Oct = t
  44. End Function

  45. '2 to 16
  46. Public Function Bin2Hex$(ByVal strBin$)
  47.     Const hS As String = "0124937FEDA5B6C8"
  48.     Const bhS As String = "0000100111101011000"
  49.     Dim i%, t$
  50.     strBin = Right$("000" & strBin, ((Len(strBin) + 3) \ 4) * 4)
  51.     For i = 1 To Len(strBin) Step 4
  52.         t = t & Mid$(hS, InStr(1, bhS, Mid$(strBin, i, 4), vbTextCompare), 1)
  53.     Next
  54.     If (Len(t) And 1) = 1 Then t = "0" & t
  55.     Bin2Hex = t
  56. End Function

  57. '2 to 10
  58. Public Function Bin2Dec&(ByVal strBin$)
  59.     Bin2Dec = CLng("&H0" & Bin2Hex(strBin))
  60. End Function

  61. '16 to 10
  62. Public Function Hex2Dec&(ByVal strHex$)
  63.     Hex2Dec = CLng("&H0" & strHex)
  64. End Function

  65. '16 to 8
  66. Public Function Hex2Oct$(ByVal strHex$)
  67.     Hex2Oct = Bin2Oct(Hex2Bin(strHex))
  68. End Function

  69. '8 to 16
  70. Public Function Oct2Hex$(ByVal strOct$)
  71.     Oct2Hex = Bin2Hex(Oct2Bin(strOct))
  72. End Function

  73. '8 to 10
  74. Public Function Oct2Dec&(ByVal strOct$)
  75.     Oct2Dec = Hex2Dec(Oct2Hex(strOct))
  76. End Function
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多