-
Notifications
You must be signed in to change notification settings - Fork 1
/
Binary to Denary Conversion October Monthly.vb
49 lines (40 loc) · 1.58 KB
/
Binary to Denary Conversion October Monthly.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Module Module1
Sub Main()
Dim validBinaryString As Boolean = True
Dim binaryString As String = ""
Dim loop1 As Integer = 0
Dim stringlength As Integer = 0
Dim stringchar As Char = ""
'Declaration for binary calculation
Dim binarychartodenary As Integer = 0
Dim denarysum As Integer = 0
Dim binaryvalue As Integer = 0
Dim binaryloop As Integer = 0
Console.Write("Enter Binary Number: ")
binaryString = Console.ReadLine
'For length validation check
stringlength = Len(binaryString)
If stringlength < 1 Or stringlength > 8 Then
validBinaryString = False
End If
'Validation check for whether string contains only 0's and 1's
For loop1 = 1 To Len(binaryString)
stringchar = Mid(binaryString, loop1, 1)
If stringchar <> "0" And stringchar <> "1" Then
validBinaryString = False
End If
Next
If validBinaryString = False Then
Console.WriteLine("Not a valid binary number... ")
Console.ReadKey()
End If
For binaryloop = 1 To stringlength
stringchar = Mid(binaryString, binaryloop, 1)
binaryvalue = Val(stringchar)
binarychartodenary = (2 ^ (stringlength - binaryloop)) * binaryvalue
denarysum = denarysum + binarychartodenary
Next
Console.WriteLine(binaryString & " = " & denarysum)
Console.ReadKey()
End Sub
End Module