Manu68
Sep 22, 2007, 07:36 PM
Hello
I need to create a converter from decimal to binary but the way it works it is not the normal way, if for example I enter the number 80, I do not want to have this number in binary (1010000) but I want to have 8 in binary and then add the 0 in binary to it, which will be 100000000. Then I will need to separate this number in groups of 4 (10000 0000) and compare the first 3 numbers of each generated number to a certain pattern to see if it is correct or not. The number entered can be any size and any decimal number. For the first part I wrote this code but it does not work:
Private Sub Command1_Click()
Dim num As String
Dim I As Integer
Dim numero
num = Text1.Text
I = 1
Do While I <= Len(num)
numero = Mid(num, I, 1)
Label1.Caption = LongToBin(numero)
I = I + 1
Loop
End Sub
Function LongToBin(ByVal bin_num As Long) As String
Dim text As String
Dim I As Integer
Do While bin_num >= 1
If bin_num Mod 2 = 1 Then
text = "1" & text
Else
text = "0" & text
End If
bin_num = bin_num \ 2
Loop
LongToBin = text
If bin_num = 0 Then
text = text + "0000"
End If
End Function
Can someone help me with this please?
Thanks a lot
I need to create a converter from decimal to binary but the way it works it is not the normal way, if for example I enter the number 80, I do not want to have this number in binary (1010000) but I want to have 8 in binary and then add the 0 in binary to it, which will be 100000000. Then I will need to separate this number in groups of 4 (10000 0000) and compare the first 3 numbers of each generated number to a certain pattern to see if it is correct or not. The number entered can be any size and any decimal number. For the first part I wrote this code but it does not work:
Private Sub Command1_Click()
Dim num As String
Dim I As Integer
Dim numero
num = Text1.Text
I = 1
Do While I <= Len(num)
numero = Mid(num, I, 1)
Label1.Caption = LongToBin(numero)
I = I + 1
Loop
End Sub
Function LongToBin(ByVal bin_num As Long) As String
Dim text As String
Dim I As Integer
Do While bin_num >= 1
If bin_num Mod 2 = 1 Then
text = "1" & text
Else
text = "0" & text
End If
bin_num = bin_num \ 2
Loop
LongToBin = text
If bin_num = 0 Then
text = text + "0000"
End If
End Function
Can someone help me with this please?
Thanks a lot