Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2017. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Binary to Decimal Converter
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 27 JUN 2017
- '
- ' Description: Visual Basic Console app to convert a binary string into a
- ' decimal value.
- ' Documentation is at:
- ' App's Visual Basic .NET code is at https://pastebin.com/J2Ch9cb8
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Module MainLIne
- Sub Main()
- ' Set Console attributes.
- Console.Title = "Binary to Decimal Converter"
- Console.ForegroundColor = ConsoleColor.Yellow
- Console.BackgroundColor = ConsoleColor.Blue
- Console.Clear()
- ' Prime read
- Dim binaryInput As String = GetValidInput()
- Do Until String.IsNullOrWhiteSpace(binaryInput)
- Dim numericValue As Double = 0
- Dim binLength = binaryInput.Length
- For idx As Integer = 2 To binLength
- If binaryInput.Substring(binLength - idx, 1) = "1" Then
- numericValue += (2 ^ (idx - 1))
- End If
- Next
- If binaryInput.Substring(binLength - 1, 1) = "1" Then
- numericValue += 1
- End If
- Console.WriteLine("Decimal value is: {0}", numericValue.ToString)
- Console.WriteLine()
- binaryInput = GetValidInput()
- Loop
- End Sub
- Private Function GetValidInput() As String
- Dim input As String = String.Empty
- Dim validData As Boolean
- Do
- validData = True
- Console.WriteLine("Press [Enter] key to exit or ...")
- Console.Write("Enter a binary string to be converted to decimal value: ")
- input = Console.ReadLine()
- For idx As Integer = 0 To input.Length - 1
- Dim tempString As String = input.Substring(idx, 1)
- If tempString <> "0" AndAlso tempString <> "1" Then
- Console.WriteLine("Enter only 0's and 1's. Try again.")
- Console.WriteLine()
- validData = False
- Exit For
- End If
- Next
- Loop Until validData
- Return input
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement