Title | Convert text into a binary format and vice versa in Visual Basic .NET |
Description | This example shows how to convert text into a binary format and vice versa in Visual Basic .NET. |
Keywords | binary, ASCII, shirts, joke, format, VB.NET |
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 btnBinaryToAscii_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnBinaryToAscii.Click
Dim bin As String = txtBinary.Text
Dim result As String = ""
For i As Integer = 0 To bin.Length - 1 Step 8
Dim next_char As String = bin.Substring(i, 8)
Dim ascii As Long = BinaryToLong(next_char)
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
' Remove any leading &B if present.
' (Note: &B is not a standard prefix, it just
' makes some sense.)
binary_value = binary_value.Trim().ToUpper()
If binary_value.StartsWith("&B") Then binary_value = _
binary_value.Substring(2)
' Strip out spaces in case the bytes are separated
' by spaces.
binary_value = binary_value.Replace(" ", "")
' Left pad with zeros so we have a full 64 bits.
binary_value = binary_value.PadLeft(64, "0")
' Read the bits in nibbles from left to right.
' (A nibble is half a byte. No kidding!)
Dim hex_result As String = ""
For nibble_num As Integer = 0 To 15
' Convert this nibble into a hexadecimal string.
Dim factor As Integer = 1
Dim nibble_value As Integer = 0
' Read the nibble's bits from right to left.
For bit As Integer = 3 To 0 Step -1
If binary_value.Substring(nibble_num * 4 + bit, _
_
1).Equals("1") Then
nibble_value += factor
End If
factor *= 2
Next bit
' Add the nibble's value to the right of the
' result hex string.
hex_result &= nibble_value.ToString("X")
Next nibble_num
' Convert the result string into a long.
Return Long.Parse(hex_result, _
Globalization.NumberStyles.HexNumber)
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 btnAsciiToBinary_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnAsciiToBinary.Click
Dim txt As String = txtAscii.Text
Dim result As String = ""
For i As Integer = 0 To txt.Length - 1
Dim bin As String = _
LongToBinary(Asc(txt.Substring(i, 1)))
result &= bin.Substring(bin.Length - 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
' Convert into hex.
Dim hex_string As String = long_value.ToString("X")
' Zero-pad to a full 16 characters.
hex_string = hex_string.PadLeft(16, "0")
' Read the hexadecimal digits
' one at a time from right to left.
Dim result_string As String = ""
For digit_num As Integer = 0 To 15
' Convert this hexadecimal digit into a
' binary nibble.
Dim digit_value As Integer = _
Integer.Parse(hex_string.Substring(digit_num, _
1), Globalization.NumberStyles.HexNumber)
' Convert the value into bits.
Dim factor As Integer = 8
Dim nibble_string As String = ""
For bit As Integer = 0 To 3
If digit_value And factor Then
nibble_string &= "1"
Else
nibble_string &= "0"
End If
factor \= 2
Next bit
' Add the nibble's string to the left of the
' result string.
result_string &= nibble_string
Next digit_num
' Add spaces between bytes if desired.
If separate_bytes Then
Dim tmp As String = ""
For i As Integer = 0 To result_string.Length - 8 _
Step 8
tmp &= result_string.Substring(i, 8) & " "
Next i
result_string = tmp.Substring(0, tmp.Length - 1)
End If
' Return the result.
Return result_string
End Function
|
|
|
|