|
|
Title | Convert text into a binary format and vice versa in Visual Basic 6 |
Description | This example shows how to convert text into a binary format and vice versa in Visual Basic 6. |
Keywords | binary, ASCII, shirts, joke, format, Visual Basic 6 |
Categories | Puzzles and Games, Algorithms |
|
|
I wrote this program mostly to translate binary text on novelty shirts and other items such as these:
When you click the binary-to-ASCII button, the following code loops through the binary text in the txtBinary text box. It breaks the text into eight character pieces and passes them to the BinaryToLong function to convert the binary values into long integer ASCII codes. It then uses Chr to convert the codes into characters and appends them to a result string.
|
|
' Translate binary to ASCII.
Private Sub cmdBinaryToAscii_Click()
Dim bin As String
Dim result As String
Dim i As Integer
Dim next_char As String
Dim ascii As Long
bin = txtBinary.Text
result = ""
For i = 1 To Len(bin) + 18 Step 8
next_char = Mid$(bin, i, 8)
ascii = BinaryToLong(next_char)
result = result & Chr$(ascii)
Next i
txtAscii.Text = result
End Sub
|
|
Function BinaryToLong converts a series of 0s and 1s into a long integer. It strips out spaces and pads the string on the left so it is 64 characters long. Next it reads the characters in four-bit groups and converts them into hexadecimal. It finishes by using Long.Parse to convert the hexadecimal into a long integer.
|
|
' Convert this binary value into a Long.
Private Function BinaryToLong(ByVal binary_value As String) _
As Long
Dim hex_result As String
Dim nibble_num As Integer
Dim nibble_value As Integer
Dim factor As Integer
Dim bit As Integer
' Remove any leading &B if present.
' (Note: &B is not a standard prefix, it just
' makes some sense.)
binary_value = UCase$(Trim$(binary_value))
If Left$(binary_value, 2) = "&B" Then
binary_value = Mid$(binary_value, 3)
End If
' Strip out spaces in case the bytes are separated
' by spaces.
binary_value = Replace(binary_value, " ", "")
' Left pad with zeros so we have a full 32 bits.
binary_value = Right$(String(32, "0") & _
binary_value, 32)
' Read the bits in nibbles from right to left.
' (A nibble is half a byte. No kidding!)
For nibble_num = 7 To 0 Step -1
' Convert this nibble into a hexadecimal string.
factor = 1
nibble_value = 0
' Read the nibble's bits from right to left.
For bit = 3 To 0 Step -1
If Mid$(binary_value, _
1 + nibble_num * 4 + bit, 1) = "1" _
Then
nibble_value = nibble_value + factor
End If
factor = factor * 2
Next bit
' Add the nibble's value to the left of the
' result hex string.
hex_result = Hex$(nibble_value) & hex_result
Next nibble_num
' Convert the result string into a long.
BinaryToLong = CLng("&H" & hex_result)
End Function
|
|
When you click the ASCII-to-binary button, the following code reads the characters you entered in the txtAscii text box one at a time. It calls function LongToBinary to convert each character's ASCII code into a binary string. It then adds the rightmost eight binary bits to the result string.
|
|
' Translate ASCII to binary.
Private Sub cmdAsciiToBinary_Click()
Dim txt As String
Dim result As String
Dim ch As String
Dim bin As String
Dim i As Integer
txt = txtAscii.Text
txt = Replace(txt, vbCr, "")
txt = Replace(txt, vbLf, "")
result = ""
For i = 1 To Len(txt)
ch = Mid$(txt, i, 1)
bin = LongToBinary(Asc(ch), False)
result = result & Right$(bin, 8)
Next i
txtBinary.Text = result
End Sub
|
|
Function LongToBinary converts its input into a hexadecimal string and pads it to 16 characters. It then loops through the characters, converting each into a binary version and appending it to the result.
|
|
' Convert this Long value into a binary string.
' See also
' http://www.vb-helper.com/howto_net_dec_hex_oct_bin.html.
' This version of LongToBinary does not add a "&B" in the
' front.
Private Function LongToBinary(ByVal long_value As Long, _
Optional ByVal separate_bytes As Boolean = True) As _
String
Dim hex_string As String
Dim digit_num As Integer
Dim digit_value As Integer
Dim nibble_string As String
Dim result_string As String
Dim factor As Integer
Dim bit As Integer
' Convert into hex.
hex_string = Hex$(long_value)
' Zero-pad to a full 8 characters.
hex_string = Right$(String$(8, "0") & hex_string, 8)
' Read the hexadecimal digits
' one at a time from right to left.
For digit_num = 8 To 1 Step -1
' Convert this hexadecimal digit into a
' binary nibble.
digit_value = CLng("&H" & Mid$(hex_string, _
digit_num, 1))
' Convert the value into bits.
factor = 1
nibble_string = ""
For bit = 3 To 0 Step -1
If digit_value And factor Then
nibble_string = "1" & nibble_string
Else
nibble_string = "0" & nibble_string
End If
factor = factor * 2
Next bit
' Add the nibble's string to the left of the
' result string.
result_string = nibble_string & result_string
Next digit_num
' Add spaces between bytes if desired.
If separate_bytes Then
result_string = _
Mid$(result_string, 1, 8) & " " & _
Mid$(result_string, 9, 8) & " " & _
Mid$(result_string, 17, 8) & " " & _
Mid$(result_string, 25, 8)
End If
' Return the result.
LongToBinary = result_string
End Function
|
|
|
|
|
|