Results 1 to 12 of 12

Thread: [RESOLVED!] Need an explanation for this (decimal -> binary)

  1. #1

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

    Talking [RESOLVED!] Need an explanation for this (decimal -> binary)

    Hi, i want to convert decimal numbers to binary numbers.
    this code here is working, only I dont understand it really... can sb explain
    what this code does exactally?

    and 2de question, I just can't find a code wich converts binary to decimal
    sb knows a code for it???

    Code:
    Dim Number As Integer
    Dim Binary_String As String 
    Number = Val(Text1.Text)
    Binary_String = ""
    While Number > 0    
    Binary_String = Str(Number Mod 2) & Binary_String    
    Number = Number \ 2
    Wend
    Text2.Text = Binary_String
    Last edited by ///Jeffrey\\\; Mar 18th, 2007 at 12:52 PM.

  2. #2
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Need an explanation for this (decimal -> binary)

    VB Code:
    1. Dim Number As Integer
    2. Dim Binary_String As String
    3.  
    4. Number = Val(Text1.Text)
    5. Binary_String = ""
    6. Do While Number > 0
    7.     Binary_String = Number Mod 2 & Binary_String
    8.     Number = Number \ 2
    9. Loop
    10. Text2.Text = Binary_String
    This is taking an integer and creating a string of 0s and 1s for the binary representation of that number.

    For example, decimal 13 is binary 1101.
    Within the loop, see if the last bit is 1 or 0. Number Mod 2 gives this result directly, as does Number And 1. Add this to the front of the string, then divide the number by 2, ignoring remainder.
    13 Mod 2 = 1; Binary_String = "1"
    13 \ 2 = 6; Loop
    6 Mod 2 = 0; Binary_String = "01"
    6 \ 2 = 3; Loop
    3 Mod 2 = 1; Binary_String = "101"
    3 \ 2 = 1; Loop
    1 Mod 2 = 1; Binary_String = "1101"
    1 \ 2 = 0; Exit Loop

    Bin>>Dec coming up...

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Need an explanation for this (decimal -> binary)

    let me try:
    Code:
    Dim Number As Integer
    Dim Binary_String As String 
    'copy the value of Text1 to Number
    Number = Val(Text1.Text)
    Binary_String = ""
    'start a do while until Number is Zero
    While Number > 0    
    'add a new binary digit for each do loop starting from the right, the binary is 0 if 'the Number can be divided evenly by 2, or is 1 if it can not be divided evenly. 
    Binary_String = Str(Number Mod 2) & Binary_String    
    'dived the number by 2 (Integer division)
    Number = Number \ 2
    'continue the loop
    Wend
    'put the Binary_string onto Text2.Text
    Text2.Text = Binary_String
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Need an explanation for this (decimal -> binary)

    And the convert back I'd use soemthing like this
    VB Code:
    1. Dim Number As Integer
    2. Dim Binary_String As String
    3. Dim i As Integer
    4. Number = 0
    5. i = 1
    6. Binary_String = Text2.Text
    7. While Len(Binary_String) > 0
    8. Number = Number + Val(Right(Binary_String, 1)) * i
    9. Binary_String = Left(Binary_String, Len(Binary_String) - 1)
    10. i = i * 2
    11. Wend
    12. Text1.Text = Number

    Note: You need to change the code for the decimal to binary a bit, in ordr to get ridd of the spae characters
    Use this line.
    Code:
    Binary_String = Trim(Str(Number Mod 2) & Binary_String)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Need an explanation for this (decimal -> binary)

    VB Code:
    1. Dim Binary_String As String
    2. Dim DecimalValue As Long
    3. Dim BitCount As Long
    4. Dim i As Long
    5.  
    6. Binary_String = Text1.Text
    7. BitCount = Len(Binary_String)
    8. ' Check length of string
    9. If BitCount = 0 Then
    10.   MsgBox "Please enter a binary string."
    11.   Exit Sub
    12. ElseIf BitCount > 31 Then
    13.   MsgBox "String is too long"
    14.   Exit Sub
    15. End If
    16. For i = 1 To BitCount
    17.   ' Make sure all characters are "0" or "1"
    18.   If Not Mid$(Binary_String, i, 1) Like "[01]" Then
    19.     MsgBox "Text is not binary string"
    20.     Exit Sub
    21.   End If
    22.   ' Add the value of each bit
    23.   If Mid$(Binary_String, i, 1) = "1" Then
    24.     DecimalValue = DecimalValue + 2 ^ (BitCount - i)
    25.   End If
    26. Next i
    27. ' Display result
    28. MsgBox DecimalValue
    Last edited by Logophobic; Mar 18th, 2007 at 11:42 AM.

  6. #6

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

    Re: Need an explanation for this (decimal -> binary)

    ok i am gonna try it I'll let you know if it worked by me... tnx allready!

  7. #7
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Need an explanation for this (decimal -> binary)

    Code:
    'Option Explicit
    ' Vbforums.com , By SESSI4ML
    ' Converts Decimal to Binary
    ' This is part of a larger program
    '
    Private Sub Form_Load()
        Text1.Text = gstrBinary
      
        Call Binary
    End Sub
    
    Private Sub Command1_Click()
        Call Binary
        Text1.SetFocus
    End Sub
    Sub Binary()
    If InStr(1, Text2.Text, "|") Then
        Text1.Text = ""
        Text2.Text = ""
        Text1.SetFocus
        Exit Sub
        End If
    Dim num As Double
    If Text1.Text = "" Then Exit Sub
    num = Text1.Text
    Dim a As Double
    Dim b As Double
    Dim c As Double
    Dim bin As String
    Dim ss As String
    
        d% = 32
        Text3.SelStart = 0
        Text3.Text = ""
        Text4.Text = ""
    aa:
    
        b = 2 ^ d%
        Text3.Text = Text3.Text + CStr(b) & vbCrLf
       If num > b Or b = num Then
            c = num / b
            bin = bin + "1"
            Text4.Text = Text4.Text + "1"
            num = num - b
            d% = d% - 1
            If d% = -1 Then
                Text2.Text = fBinary(bin)
                'Text4.Text = bin
                Exit Sub
            End If
         Else
            bin = bin + "0"
            Text4.Text = Text4.Text + "0"
            d% = d% - 1
            If d% = -1 Then
                Text2.Text = fBinary(bin)
               ' Text4.Text = bin
                Exit Sub
            End If
        End If
     GoTo aa
        
    
    End Sub
    Function fBinary(strBin As String) As String
        intlen = Len(strBin) + Int((Len(strBin) / 8))
        For a = 1 To intlen
            fBinary = fBinary + "0"
        Next a
        b = 0
        For a = Len(strBin) To 1 Step -1
              Mid(fBinary, intlen - b, 1) = Mid(strBin, a, 1)
                   ct = ct + 1
                If ct = 8 Then
                    b = b + 1
                    ct = 0
                    Mid(fBinary, intlen - b, 1) = "|"
                End If
                intlen = intlen - 1
        Next a
                
    End Function
    
    Private Sub Command2_Click()
        Unload Me
    '   frmCalc.Show
    End
    End Sub
    
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      Select Case KeyAscii
            Case 8                      ' back space... delete last char
                 ' Let char pass
        Case 48 To 57                   ' Digits 0-9
                ' Let char pass
           
        Case Else
            KeyAscii = 0
        End Select
    End Sub 'Process_KeyBoard

  8. #8
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Need an explanation for this (decimal -> binary)

    Dec > Bin...VB File
    Attached Files Attached Files

  9. #9

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

    Re: Need an explanation for this (decimal -> binary)

    If Not Mid$(Binary_String, i, 1) Like "[01]" Then....

    what does this code do exactally?

  10. #10

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

    Re: Need an explanation for this (decimal -> binary)

    If Mid$(Binary_String, i, 1) = "1" Then
    DecimalValue = DecimalValue + 2 ^ (BitCount - i)

    I meant this one

  11. #11
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Need an explanation for this (decimal -> binary)

    If the character doesn't match one of the charcters within the brackets Then...
    For details about the Like operator

    Hm? make up my mind!

    If the character is "1" Then the bit is ON, so add the value of that bit to our decimal value. The value of each bit is 2 to the power of BitNumber, where BitNumber = 0 for the rightmost bit, and increases to the left. Since we are counting i from the left, we must subtract i from the length of the string to know how far it is from the right.

  12. #12

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

    Re: Need an explanation for this (decimal -> binary)

    ah i get it, you really helped me! I'll set the thread to Resolved

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