|
|
Title | Use the Convert class to convert values between decimal, hexadecimal, octal, and binary in Visual Basic .NET |
Description | This example shows how to use the Convert class to convert values between decimal, hexadecimal, octal, and binary in Visual Basic .NET. |
Keywords | Convert class, convert values, decimal, hexadecimal, octal, binary, base, Visual Basic .NET, VB.NET |
Categories | VB.NET, Algorithms |
|
|
When you change a text value in any of the text boxes, the txt_TextChanged event handler takes action. That routine calls subroutine DisplayValue, passing it the text box that was modified.
Subroutine DisplayValue checks the text box's name to see how it should interpret the value. In any case, the code uses the Convert class's ToInt64 method to convert the text into a 64-bit integer. (The class has similar methods to convert to other data types, for example ToInt32.)
The optional second parameter to ToInt64 indicates the base from which to convert. This can be 2, 8, 10 (the default), or 16.
Having converted the text value into a Long, the program displays it in different bases. It uses ToString to display the decimal value, ToString with the parameter X to display the hexadecimal value, the Oct$ function to display the octal value (let me know if you know of a VB .NET method for formatting as octal), and function LongToBinary to display the binary.
|
|
Private Sub txt_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles _
txtDecimal.TextChanged, txtBinary.TextChanged, _
txtHexadecimal.TextChanged, txtOctal.TextChanged
DisplayValue(sender)
End Sub
' Display the value in the indicated control in
' the other controls.
Private Sub DisplayValue(ByVal source As TextBox)
' Don't recurse.
Static ignore_events As Boolean = False
If ignore_events Then Exit Sub
ignore_events = True
' Get the value.
Dim value As Long
Try
Select Case source.Name
Case "txtDecimal"
value = Convert.ToInt64(source.Text)
Case "txtHexadecimal"
value = Convert.ToInt64(source.Text, 16)
Case "txtOctal"
value = Convert.ToInt64(source.Text, 8)
Case "txtBinary"
value = Convert.ToInt64(source.Text.Replace(" " & _
"", ""), 2)
End Select
Catch ex As Exception
MessageBox.Show("Error parsing input" & vbCrLf & _
ex.Message, _
"Input Error", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End Try
' Display the value in different formats.
If source.Name <> "txtDecimal" Then
txtDecimal.Text = value.ToString()
End If
If source.Name <> "txtHexadecimal" Then
txtHexadecimal.Text = value.ToString("X")
End If
If source.Name <> "txtOctal" Then
txtOctal.Text = Oct$(value)
End If
If source.Name <> "txtBinary" Then
txtBinary.Text = LongToBinary(value)
End If
ignore_events = False
End Sub
|
|
Function LongToBinary returns a binary text representation of a value. It also considers the value nibble-by-nibble. For each nibble, the code compares the nibble's value to a power of 2 and outputs a 0 or 1 depending on whether the nibble has the same bit set as the power of two.
|
|
' Convert this Long value into a binary string.
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 = New String("0", 16 - hex_string.Length) & _
hex_string
' 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
|
|
|
|
|
|