博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

VB实现各种进制之间的转换

Posted on 2005-06-21 09:14  黄金海岸  阅读(1018)  评论(0编辑  收藏  举报
Public Function Bin2Oct(num)
    Bin2Oct = Oct(Bin2Dec(num))
End Function

Public Function Bin2Dec(num)
    Dim a, i, b
    a = Len(num)
    For i = 0 To a - 1
        b = b + 2 ^ i * Mid(num, a - i, 1)
    Next
   
    Bin2Dec = b
End Function

Public Function Bin2Hex(num)
    Bin2Hex = Hex(Bin2Dec(num))
End Function

Public Function Oct2Bin(num)
    Oct2Bin = Dec2Bin(CInt("&O" & num))
End Function

Public Function Oct2Dec(num)
    Oct2Dec = CInt("&O" & num)
End Function

Public Function Oct2Hex(num)
    Oct2Hex = Hex("&O" & num)
End Function

Public Function Dec2Bin(num)
    Dim a, b
    a = num
    Do
        If a Mod 2 = 0 Then
            b = "0" & b
        Else
            b = "1" & b
        End If
        a = Int(a / 2)
    Loop Until a = 0
   
    Dec2Bin = b
End Function

Public Function Dec2Oct(num)
    Dec2Oct = Oct(num)
End Function

Public Function Dec2Hex(num)
    Dec2Hex = Hex(num)
End Function

Public Function Hex2Bin(num)
    Hex2Bin = Dec2Bin(CInt("&H" & num))
End Function

Public Function Hex2Oct(num)
    Hex2Oct = Oct("&H" & num)
End Function

Public Function Hex2Dec(num)
    Hex2Dec = CInt("&H" & num)
End Function