[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 :sick:
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
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
Bin>>Dec coming up...
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
Re: Need an explanation for this (decimal -> binary)
And the convert back I'd use soemthing like this
VB Code:
Dim Number As Integer
Dim Binary_String As String
Dim i As Integer
Number = 0
i = 1
Binary_String = Text2.Text
While Len(Binary_String) > 0
Number = Number + Val(Right(Binary_String, 1)) * i
Binary_String = Left(Binary_String, Len(Binary_String) - 1)
i = i * 2
Wend
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)
Re: Need an explanation for this (decimal -> binary)
VB Code:
Dim Binary_String As String
Dim DecimalValue As Long
Dim BitCount As Long
Dim i As Long
Binary_String = Text1.Text
BitCount = Len(Binary_String)
' Check length of string
If BitCount = 0 Then
MsgBox "Please enter a binary string."
Exit Sub
ElseIf BitCount > 31 Then
MsgBox "String is too long"
Exit Sub
End If
For i = 1 To BitCount
' Make sure all characters are "0" or "1"
If Not Mid$(Binary_String, i, 1) Like "[01]" Then
MsgBox "Text is not binary string"
Exit Sub
End If
' Add the value of each bit
If Mid$(Binary_String, i, 1) = "1" Then
DecimalValue = DecimalValue + 2 ^ (BitCount - i)
End If
Next i
' Display result
MsgBox DecimalValue
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!
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
1 Attachment(s)
Re: Need an explanation for this (decimal -> binary)
Re: Need an explanation for this (decimal -> binary)
If Not Mid$(Binary_String, i, 1) Like "[01]" Then....
what does this code do exactally?
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 :)
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! :D
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.
Re: Need an explanation for this (decimal -> binary)
ah i get it, you really helped me! I'll set the thread to Resolved :D