码迷,mamicode.com
首页 > 其他好文 > 详细

16进制转二进制、八进制

时间:2015-09-06 12:26:19      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

‘ 用途:将二进制转化为八进制
‘ 输入:Bin(二进制数)
‘ 输入数据类型:String
‘ 输出:BIN_to_OCT(八进制数)
‘ 输出数据类型:String
‘ 输入的最大数为2147483647个字符
Public Function BIN_to_OCT(ByVal Bin As String) As String
    Dim i As Long
    Dim H As String
    If Len(Bin) Mod 3 <> 0 Then
        Bin = String(3 - Len(Bin) Mod 3, "0") & Bin
    End If
    
    For i = 1 To Len(Bin) Step 3
        Select Case Mid(Bin, i, 3)
            Case "000": H = H & "0"
            Case "001": H = H & "1"
            Case "010": H = H & "2"
            Case "011": H = H & "3"
            Case "100": H = H & "4"
            Case "101": H = H & "5"
            Case "110": H = H & "6"
            Case "111": H = H & "7"
        End Select
    Next i
    While Left(H, 1) = "0"
        H = Right(H, Len(H) - 1)
    Wend
    BIN_to_OCT = H
End Function

‘ 用途:将十六进制转化为二进制
‘ 输入:Hex(十六进制数)
‘ 输入数据类型:String
‘ 输出:HEX_to_BIN(二进制数)
‘ 输出数据类型:String
‘ 输入的最大数为2147483647个字符
Public Function HEX_to_BIN(ByVal Hex As String) As String
    Dim i As Long
    Dim B As String
    
    Hex = UCase(Hex)
    For i = 1 To Len(Hex)
        Select Case Mid(Hex, i, 1)
            Case "0": B = B & "0000"
            Case "1": B = B & "0001"
            Case "2": B = B & "0010"
            Case "3": B = B & "0011"
            Case "4": B = B & "0100"
            Case "5": B = B & "0101"
            Case "6": B = B & "0110"
            Case "7": B = B & "0111"
            Case "8": B = B & "1000"
            Case "9": B = B & "1001"
            Case "A": B = B & "1010"
            Case "B": B = B & "1011"
            Case "C": B = B & "1100"
            Case "D": B = B & "1101"
            Case "E": B = B & "1110"
            Case "F": B = B & "1111"
        End Select
    Next i
    While Left(B, 1) = "0"
        B = Right(B, Len(B) - 1)
    Wend
    HEX_to_BIN = B
End Function

‘ 用途:将十六进制转化为八进制
‘ 输入:Hex(十六进制数)
‘ 输入数据类型:String
‘ 输出:HEX_to_OCT(八进制数)
‘ 输出数据类型:String
‘ 输入的最大数为2147483647个字符
Public Function HEX_to_OCT(ByVal Hex As String) As String
    Dim Bin As String
    Hex = UCase(Hex)
    Bin = HEX_to_BIN(Hex)
    HEX_to_OCT = BIN_to_OCT(Bin)
End Function

16进制转二进制、八进制

标签:

原文地址:http://www.cnblogs.com/lbnnbs/p/4784839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!