Code: Select all
Module Module1
Private BinaryString, binr, choice, signed_or_unsigned, HexString, first_hex, hexr, pie As String
Private digit, remain, quit, negative, oor, bits, disable_welcome_text, min_num, max_num, confirm As Double
'Private pos As Double
'uncomment the above if you are using Microsoft Visual Basic .NET 2005 Express
Sub Main()
display_options()
program()
End Sub
Private Sub program()
Console.WriteLine("(for a list of options, type n)")
Console.WriteLine("What do you want to do?:")
disable_welcome_text = 1
choice = Console.ReadLine()
Select Case choice
Case "q"
quit = 1
Case "n"
display_options()
Case "bin2dec"
validate_bits()
Console.WriteLine("Signed or unsigned? If signed, use two's compliment")
validate_sign()
determine_range()
validate_BinaryString()
bin2dec()
Console.WriteLine(digit)
BinaryString = ""
binr = ""
digit = 0
Case "dec2bin"
validate_bits()
Console.WriteLine("Signed or unsigned? (signed will be in two's compiment)")
validate_sign()
determine_range()
validate_digit()
BinaryString = ""
binr = ""
dec2bin()
Console.WriteLine(BinaryString)
BinaryString = ""
binr = ""
digit = 0
Case "dec2hex"
validate_bits()
Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
validate_sign()
determine_range()
validate_digit()
BinaryString = ""
binr = ""
dec2hex()
Console.WriteLine(HexString)
BinaryString = ""
binr = ""
digit = 0
Case "bin2hex"
validate_bits()
Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
validate_sign()
validate_BinaryString()
bin2hex()
Console.WriteLine(HexString)
Case "hex2bin"
validate_bits()
Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
validate_sign()
'validate_hex()
hex2bin()
Console.WriteLine(BinaryString)
Case "hex2dec"
validate_bits()
Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
validate_sign()
'validate_hex()
hex2dec()
Console.WriteLine(digit)
End Select
program()
End Sub
Private Sub determine_range()
Select Case signed_or_unsigned
Case "signed"
min_num = (-(2 ^ (bits - 1)))
max_num = ((2 ^ (bits - 1)) - 1)
Case "unsigned"
min_num = 0
max_num = ((2 ^ bits) - 1)
End Select
End Sub
Private Sub validate_sign()
Do Until confirm = 1
signed_or_unsigned = Console.ReadLine()
Select Case signed_or_unsigned
Case "signed"
confirm = 1
Case "unsigned"
confirm = 1
End Select
Loop
confirm = 0
End Sub
Private Sub validate_digit()
Console.WriteLine("Enter decimal number: ")
Do Until confirm = 1
digit = Console.ReadLine()
If digit < min_num Or digit > max_num Then
Console.WriteLine("Invalid data! Not within range!")
End If
If (digit - Int(digit)) <> 0 Then
Console.WriteLine("Invalid data! Digit must be an integer, not a float!")
End If
If digit < min_num Or digit > max_num Or (digit - Int(digit)) <> 0 Then
Console.WriteLine("Try again:")
End If
If signed_or_unsigned = "signed" Then
If digit < (max_num + 1) And digit > (min_num - 1) Then
If digit < 0 Then
oor = (digit + (2 ^ (bits - 1)))
digit = ((2 ^ (bits - 1)) + oor)
oor = 0
End If
confirm = 1
End If
End If
If signed_or_unsigned = "unsigned" Then
If digit < (max_num + 1) And digit > -1 Then
confirm = 1
End If
End If
Loop
confirm = 0
End Sub
Private Sub validate_BinaryString()
Console.WriteLine("Enter binary string: ")
Do Until confirm = 1
BinaryString = Console.ReadLine()
For pos = BinaryString.Length To 1 Step -1
If Mid(BinaryString, pos, 1) <> "1" And Mid(BinaryString, pos, 1) <> "0" Then
oor = 1
End If
Next
If BinaryString.Length > bits Or BinaryString = "" Or oor = 1 Then
If BinaryString.Length > bits Then
Console.WriteLine("Invalid data! Bit length out of range!")
End If
If BinaryString = "" Then
Console.WriteLine("Invalid data! BinaryString cannot be empty!")
End If
If oor = 1 Then
Console.WriteLine("Invalid data! Some of the characters are not 1's and 0's!")
End If
End If
If confirm <> 1 And oor <> 1 Then
Console.WriteLine("Try again:")
End If
If BinaryString.Length < (bits + 1) And oor <> 1 And BinaryString <> "" Then
confirm = 1
End If
oor = 0
Loop
confirm = 0
End Sub
Private Sub validate_bits()
Console.WriteLine("Address space (i.e. max amount of bits):")
Do Until confirm = 1
bits = Console.ReadLine()
If bits < 1 Or (digit - Int(digit)) <> 0 Then
If bits < 1 Then
Console.WriteLine("Invalid data! Bit length cannot be 0 or negative!")
End If
If (digit - Int(digit)) <> 0 Then
Console.WriteLine("Invalid data! Bit length cannot be a float! It *must* be an integer!")
End If
Console.WriteLine("Try again:")
End If
If bits > 0 Then
confirm = 1
End If
Loop
confirm = 0
End Sub
Private Sub validate_hex()
Console.WriteLine("Enter hexadecimal string:")
Do Until confirm = 1
hex2bin()
bin2dec()
If digit < min_num Or digit > max_num Then
Console.WriteLine("Invalid data! Not within range!")
Else
confirm = 1
End If
Loop
confirm = 0
End Sub
Private Sub bin2dec()
For pos = BinaryString.Length To 1 Step -1
If Mid(BinaryString, pos, 1) = "1" Then
digit = digit + (2 ^ (BinaryString.Length - pos))
End If
Next
If signed_or_unsigned = "signed" And BinaryString.Length = bits Then
If Mid(BinaryString, 1, 1) = "1" Then
digit = digit - (2 ^ bits)
End If
End If
End Sub
Private Sub dec2bin()
Do Until digit = 0
remain = (digit / 2) - Int(digit / 2)
If remain > 0 Then
binr = binr + "1"
Else
binr = binr + "0"
End If
digit = (digit / 2) - remain
Loop
For pos = binr.Length To 1 Step -1
BinaryString = BinaryString + Mid(binr, pos, 1)
Next
End Sub
Private Sub dec2hex()
dec2bin()
binr = ""
oor = 0
bin2hex()
End Sub
Private Sub bin2hex()
If BinaryString.Length <> bits Then
oor = bits - BinaryString.Length
For pos = 1 To oor Step 1
binr = binr + "0"
Next
oor = 0
BinaryString = binr + BinaryString
End If
oor = 0
binr = ""
For pos = 1 To BinaryString.Length Step 4
Select Case Mid(BinaryString, pos, 4)
Case "0000"
HexString = HexString + "0"
Case "0001"
HexString = HexString + "1"
Case "0010"
HexString = HexString + "2"
Case "0011"
HexString = HexString + "3"
Case "0100"
HexString = HexString + "4"
Case "0101"
HexString = HexString + "5"
Case "0110"
HexString = HexString + "6"
Case "0111"
HexString = HexString + "7"
Case "1000"
HexString = HexString + "8"
Case "1001"
HexString = HexString + "9"
Case "1010"
HexString = HexString + "A"
Case "1011"
HexString = HexString + "B"
Case "1100"
HexString = HexString + "C"
Case "1101"
HexString = HexString + "D"
Case "1110"
HexString = HexString + "E"
Case "1111"
HexString = HexString + "F"
End Select
Next
End Sub
Private Sub hex2bin()
For pos = 1 To HexString.Length Step 1
Select Case Mid(HexString, pos, 1)
Case "0"
BinaryString = BinaryString + "0000"
Case "1"
BinaryString = BinaryString + "0001"
Case "2"
BinaryString = BinaryString + "0010"
Case "3"
BinaryString = BinaryString + "0011"
Case "4"
BinaryString = BinaryString + "0100"
Case "5"
BinaryString = BinaryString + "0101"
Case "6"
BinaryString = BinaryString + "0110"
Case "7"
BinaryString = BinaryString + "0111"
Case "8"
BinaryString = BinaryString + "1000"
Case "9"
BinaryString = BinaryString + "1001"
Case "A" Or "a"
BinaryString = BinaryString + "1010"
Case "B" Or "b"
BinaryString = BinaryString + "1011"
Case "C" Or "c"
BinaryString = BinaryString + "1100"
Case "D" Or "d"
BinaryString = BinaryString + "1101"
Case "E" Or "e"
BinaryString = BinaryString + "1110"
Case "F" Or "f"
BinaryString = BinaryString + "1111"
End Select
Next
End Sub
Private Sub hex2dec()
hex2bin()
bin2dec()
End Sub
Private Sub display_options()
Console.WriteLine("dec2bin - Decimal to Binary Conversion")
Console.WriteLine("dec2hex - Decimal to hexadecimal conversion (finished but untested)")
Console.WriteLine("bin2dec - Binary to Decimal conversion")
Console.WriteLine("bin2hex - Binary to Hexadecimal conversion (finished but untested)")
Console.WriteLine("hex2dec - hexadecimal to decimal conversion (currently not started)")
Console.WriteLine("hex2bin - hexadecimal to binary conversion (currently not started)")
Console.WriteLine("q - exit this program")
End Sub
End Module