我刚 在下面的帖中回答了的。
你去看看吧
http://www.csdn.net/expert/topic/670/670623.xml?temp=4.366702E-02

解决方案 »

  1.   

    '2进制转化为10进制
    Dim tempmul, tempplus As Double   'tempmul作累乘器,tempplus作累加器
    Dim i, j As Integer
    tempplus = 0
    For i = 1 To Len(str)              '以2为基,逐位按权相加
      If Mid(str, i, 1) = "1" Then
         tempmul = 1
         For j = 1 To Len(str) - i
             tempmul = tempmul * 2
         Next j
         tempplus = tempplus + tempmul
      End If
    Next i
    btocresult = tempplus
    Dim bit4 As String
    Dim tempresult As String
    Dim i As Integer
    tempresult = ""
    For i = 1 To Len(str) / 4
      bit4 = Mid(str, (i - 1) * 4 + 1, 4)
      Select Case bit4
      Case "0000"
      tempresult = tempresult & "0"
      Case "0001"
      tempresult = tempresult & "1"
      Case "0010"
      tempresult = tempresult & "2"
      Case "0011"
      tempresult = tempresult & "3"
      Case "0100"
      tempresult = tempresult & "4"
      Case "0101"
      tempresult = tempresult & "5"
      Case "0110"
      tempresult = tempresult & "6"
      Case "0111"
      tempresult = tempresult & "7"
      Case "1000"
      tempresult = tempresult & "8"
      Case "1001"
      tempresult = tempresult & "9"
      Case "1010"
      tempresult = tempresult & "a"
      Case "1011"
      tempresult = tempresult & "b"
      Case "1100"
      tempresult = tempresult & "c"
      Case "1101"
      tempresult = tempresult & "d"
      Case "1110"
      tempresult = tempresult & "e"
      Case "1111"
      tempresult = tempresult & "f"
      End Select
    Next i
    messageh = tempresult
      

  2.   

    用函数Oct()将十进制位数转换为八进制,用Hex()将十进制位数转换为十六进制,不过值得注意的是转换后数字型变为字符型。如果是一个变量我们可以用如下代码完成十进制向其他进制转换的目的。
        dim ANumaslong
        ANum=&O10′&O是八进制的表示符号ANum自行转换为8。
        ANum=&HA′&H是十六进制的表示符号ANum自行转换为10。
        如果我们有一个字符变量Astring为八进制的或十六进制的,用ANum=″&O″+Astring和ANum=″&H″+Astring能将它转换为十进制。
        VB中我没有找到二进制的转换函数,用如下代码可以实现十进制到二进制的转换。
        OptionExplicit
        Private Function TenturnTwo(ByValvarNumAsLong)   
         Dim returnStringAsString,ModNumAsInteger
        Do WhilevarNum>0
        ModNum=varNumMod2
        varNum=varNum\2
        returnString=Trim(Str(ModNum))+returnString
        Loop
        TenturnTwo=returnString
        End Function
        Private Function TwoturnTen(ByValvarStringAsString)
        DimSLenAsLong,I As Long,returnNumAsLong
        SLen=Len(varString)
        ForI=0ToSLen-1
        returnNum=returnNum+Val(Mid(varString,I+1,1))*(2^(SLen-I-1))
        Next
        TwoturnTen=returnNum
        End Function
      

  3.   

    这里面什么都有。 你试试吧:
    ==================================================================
    Option Explicit
    Public Function HexToBin(InputVal As String)'This function accepts an Input Value as a Hex(base16) value
    'and first converts it to a decimal by using the Val() Function
    'after converting it to decimal, it calls the DecToBin() Function
    'which converts a decimal to a binary output.    HexToBin = DecToBin(Val("&H" & InputVal & "&"))End FunctionPublic Function HexToDec(InputVal As String)'This function accepts an input as a hex value and converts it to
    'to a decimal value using the val() function for output.    HexToDec = Val("&H" & InputVal & "&")End FunctionPublic Function DecToHex(InputVal As String)'Converts a decimal value to hexidecimal using the hex() Function.    DecToHex = Hex(InputVal)
        
    End FunctionPublic Function DecToBin(InputVal As String)'This function accepts a decimal value as an input and calculates the binary
    'equivalent as the function's outputDim X As Integer
    Dim Y As Integer
    Dim InitVal As Integer
    Dim Counter As Integer
    Dim Pos(100) As String
    Dim Output As String
    Dim NewOutPut As String'initialize 100 element array to '0'
    For X = 0 To UBound(Pos)
        Pos(X) = "0"
    Next'set x=0 so that we may reuse this integer
    X = 0'Store the inputval in another variable so that we can rip it apart in
    'the next Do Until... Loop.
    InitVal = InputVal'find out how many digits are in the binary output, store the value in
    'the variable 'Counter'
    Do Until InitVal = 1
        InitVal = Abs(InitVal / 2)
        Counter = Counter + 1
    Loop
        
    'Convert an input value in Decimal to it's Binary
    'equivalent, by finding the highest value that can be
    'used as an exponent of two, subtracting the value
    'and equating that value as a position with a value
    'of '1'. (ie. 7: (2^2=4, 7-4=3, 2^1=2, 3-2=1, 2^0=1)
    'Therefore positions 2, 1, and 0 are ones: 111
    'This formula is insane... The 'Pos()' array stores a value
    'of '1' in each element that represents the positional
    'notation of the binary output.Do Until InputVal = 0
        
        'Find the highest exponent of 2 that can be used against our input
        'this number will represent a position in the output that will contain
        'a value of '1'.
        Do Until X >= CInt(InputVal)
            X = 2 ^ Y
            Y = Y + 1
        Loop
        
        'If X is higher than the input, then we have to subtract 2 from Y cause the
        'above loop incremented too many times. If it's equal, we only have to
        'subtract one to obtain our desired value. Once the value of 'Y' is
        'established, set Pos(Y) = '1'.
        If X > CInt(InputVal) Then
            Y = Y - 2
            Pos(Y) = "1"
        Else
            Y = Y - 1
            Pos(Y) = "1"
        End If
        
        'Subtract the result of our final formula from the input value, reinitialize
        'X and Y to equal 0 and go back into the loop with the new InputVal to find
        'the next position of '1'.
        InputVal = InputVal - (2 ^ Y)
        
        X = 0
        Y = 0
        
    Loop'Reinitialize "Output" to equal null
    Output = ""'for each digit in the binary output, assign either a 1 or a 0 to Output
    'depending on the value of pos(y)
    For Y = 0 To (Counter - 1)
        Output = Output & Pos(Y)
    Next'the above loop creates the binary output and stores it in the string variable:
    '"OutPut".. only one problem, it's backwards! So, let's reverse it and store it
    'in a new variable appropriately called "NewOutPut"For Y = 0 To Len(Output)
        NewOutPut = NewOutPut & Right(Output, 1)
        If Len(Output) > 0 Then
            Output = Left(Output, Len(Output) - 1)
        End If
    Next'Remove any '0's from the right of the string since these are useless.Do Until Left(NewOutPut, 1) = "1"
        NewOutPut = Right(NewOutPut, Len(NewOutPut) - 1)
    LoopDecToBin = NewOutPutEnd FunctionPublic Function OctToDec(InputVal As String)Dim str_octLengthCount As Integer
    Dim octTodecOutput As Double'Accepts an Octal Value and converts it to Decimal by taking the
    'rightmost digit and multiplying it by 8 to the power of it's
    'position. (ie.. 12 = (1 * 8^1) + (2 * 8^0) = 10).
    'Truncates the rightmost character of the input value and continues
    'until len(inputval) = 0.Do Until Len(InputVal) = 0
        octTodecOutput = octTodecOutput + _
                        Right(InputVal, 1) * 8 ^ str_octLengthCount
        InputVal = Left(InputVal, Len(InputVal) - 1)
        str_octLengthCount = str_octLengthCount + 1
    LoopOctToDec = octTodecOutputEnd FunctionPublic Function BinToDec(InputVal As String)Dim X As Integer
    Dim A As Integer
    Dim intOutPutVal As Double'Similar to the OctToDec() Function. Takes the rightmost value,
    'multiplies it by 2 to the power of it's position, then adds the
    'values together to produce the output.
    'Truncates the rightmost character and continues.
    '(ie. 101 = (1 * 2^2) + (0 * 2^1) + (1 * 2^0) = 5)Do Until Len(InputVal) = 0
        intOutPutVal = intOutPutVal + Right(InputVal, 1) * (2 ^ X)
        InputVal = Left(InputVal, (Len(InputVal) - 1))
        X = X + 1
    LoopBinToDec = intOutPutValEnd Function