求一个0-255的十进制数或一个00-FF的16进制数的二进制各位的值?例如:十进制数64,它的二进制为01000000,那么各个位的值分别为
   
   位数    值
    1      0
    2      0
    3      0
    4      0
    5      0
    6      0
    7      1
    8      0

解决方案 »

  1.   

    Private Sub cmddec2bin_Click()Dim bytValue(1 To 8) As Byte
    Dim s As String
    Dim i As Integer
    Dim sBin As StringIf IsNumeric(txtdec2bin.Text) Then
      sbin = Dec2Bin(txtdec2bin.Text)
    End Ifs = Format(sBin, "00000000")
    For i = 1 To 8
        bytValue(i) = Val(Mid(s, 9 - i, 1))
        Debug.Print i, bytValue(i)
    NextEnd Sub
    Function Dec2Bin(InputData As Double) As String
    ''
    ''  Converts Decimal to Binary
    ''  This uses the Quotient Remainder method
    ''
    Dim Quot As Double
    Dim Remainder As Double
    Dim BinOut As String
    Dim i As Integer
    Dim NewVal As Double
    Dim TempString As String
    Dim TempVal As Double
    Dim BinTemp As String
    Dim BinTemp1 As String
    Dim PosDot As Integer
    Dim Temp2 As String
    ''  Check to see if there is a decimal point or not
    ''
    If InStr(1, CStr(InputData), ".") Then
      MsgBox "Only Whole Numbers can be converted", vbCritical
      GoTo eds
    End IfBinOut = ""
    NewVal = InputData
    DoAgain:''  Start the Calculations off
    NewVal = (NewVal / 2)
    ''  If we have a remainder
    If InStr(1, CStr(NewVal), ".") Then
      BinOut = BinOut + "1"
      
      '' Get rid of the Remainder
      NewVal = Format(NewVal, "#0")
      NewVal = (NewVal - 1)
      
       If NewVal < 1 Then
         GoTo DoneIt
       End If
    Else
      BinOut = BinOut + "0"
       If NewVal < 1 Then
         GoTo DoneIt
       End If
    End If
    GoTo DoAgainDoneIt:BinTemp = ""''  Reverse the Result
    For i = Len(BinOut) To 1 Step -1
     BinTemp1 = Mid(BinOut, i, 1)
     BinTemp = BinTemp + BinTemp1
    Next iBinOut = BinTemp'' Output the Result
    Dec2Bin = BinOut
    eds:
    End Function
      

  2.   

    这样做:
    Public Function Nible2Bin(Byval x As Integer)
        If x > 15 Or x < 0 Then Exit Function
        Nible2Bin = Choose(x + 1, "0000","0001","0010","0011", _
                                  "0100","0101","0110","0111", _
                                  "1000","1001","1010","1011", _
                                  "1100","1101","1110","1111")
    End Sub将所有数字都按半字节来处理,每字节将两个半字节连接就可以了。
      

  3.   

    Public Function Nible2Bin(Byval x As Integer) As String
      

  4.   

    Public Function huan2(ten As Double) As String
       Dim two As String
       Dim n As Double
       Do Until ten = 0
          n = ten Mod 2
          two = n & two
          ten = ten \ 2
       Loop
      huan2 = two
    End Function
      

  5.   

    private function conv(byt as byte) as string
       if byt = 0 then  exit function
       conv = conv(byt \ 2) & byt mod 2
    end function