[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.
Re: Need an explanation for this (decimal -> binary)
VB Code:
Dim Number As Integer
Dim Binary_String As String
Number = Val(Text1.Text)
Binary_String = ""
Do While Number > 0
Binary_String = Number Mod 2 & Binary_String
Number = Number \ 2
Loop
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
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!
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
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.