Results 1 to 2 of 2

Thread: Binair -> Decimal

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Location
    Netherlands
    Posts
    71

    Smile Binair -> Decimal

    HI,

    I'm making an caculator, and what I want is to convert a decimal number to
    a binair number (and backwards).
    Can give me hint how to do this, because I dont know where to start

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Binair -> Decimal

    If just whole numbers then

    Code:
    Private Sub Form_Load()
    Dim strBinary As String
       strBinary = Oct(12345)
       strBinary = Replace(strBinary, "0", "000")
       strBinary = Replace(strBinary, "1", "001")
       strBinary = Replace(strBinary, "2", "010")
       strBinary = Replace(strBinary, "3", "011")
       strBinary = Replace(strBinary, "4", "100")
       strBinary = Replace(strBinary, "5", "101")
       strBinary = Replace(strBinary, "6", "110")
       strBinary = Replace(strBinary, "7", "111")
       Debug.Print strBinary
       
    Dim lngPos As Long
    Dim strOct As String
    
       If Len(strBinary) Mod 3 > 0 Then  'add leading zeroes
          strBinary = String(3 - (Len(strBinary) Mod 3), "0") & strBinary
       End If
       strOct = "&O"
       For lngPos = 1 To Len(strBinary) Step 3
          Select Case Mid$(strBinary, lngPos, 3)
          Case "000":    strOct = strOct & "0"
          Case "001":    strOct = strOct & "1"
          Case "010":    strOct = strOct & "2"
          Case "011":    strOct = strOct & "3"
          Case "100":    strOct = strOct & "4"
          Case "101":    strOct = strOct & "5"
          Case "110":    strOct = strOct & "6"
          Case "111":    strOct = strOct & "7"
          End Select
       Next
       Debug.Print CLng(strOct)
    End Sub
    I used octet to limit test to 8 combinations instead of 16 with hex.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width