• Welcome to Valhalla Legends Archive.
 

Binary Converting.

Started by OuTLawZGoSu, March 05, 2004, 08:23 AM

Previous topic - Next topic

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"

Telos


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

OuTLawZGoSu

Aight thx.

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

Eli_1

Quote from: OuTLawZGoSu on March 05, 2004, 04:30 PM
Aight thx.

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

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.