Valhalla Legends Forums Archive | Visual Basic Programming | Binary Converting.

AuthorMessageTime
OuTLawZGoSu
How can I make a program conver letters to Binary?
Kind of like when you use ASCII converting.

I need it to convert the text in "Text1" and put the Binary text to "Text2" when I press "CmdConvert"
March 5, 2004, 2:23 PM
Telos
[code]
Private Sub cmdConvert_Click()
Dim i As Integer, j As Integer ' Counters
Dim l As Integer ' l = length of initial string
Dim t As Integer ' t = 2^x power for each bit
Dim charT As Byte ' The current character

l = Len(Text1.Text) ' Only evaluate length once

For i = 1 To l ' Cycle through each character
charT = Asc(Mid$(Text1.Text, i, 1))
' Get each character

For j = 7 To 0 Step -1 ' Cycle through each bit
t = 2 ^ j ' Set the mask
If (t And charT) = t Then ' If the bit is set
Text2.Text = Text2.Text & "1"
Else ' Otherwise
Text2.Text = Text2.Text & "0"
End If
Next j
Next i
End Sub
[/code]
March 5, 2004, 4:05 PM
OuTLawZGoSu
Aight thx.

One more thing, how can I make it so it will conver binary to regular?
March 5, 2004, 10:30 PM
Eli_1
[quote author=OuTLawZGoSu link=board=31;threadid=5611;start=0#msg47786 date=1078525840]
Aight thx.

One more thing, how can I make it so it will conver binary to regular?
[/quote]

um, I know this post is really old and I'm sorry to bring up a dead topic... but...

Random binary #:
00100111

Now break it into two sections:
0010 0111

Now, go through the numbers one by one.
If the number is a one, then:
Take 2 ^ n. Where n is the ammount of numbers to the right of it.
0 0 1 0
2^1
Add them up: 2

Now do the same for the other section.
0 1 1 1
2^2 2^1 2^0
4 2 1
Add them up: 7

So if I did it right, 00100111 is 0x27
Chr(&H27) is your character.
May 8, 2004, 3:39 AM

Search