Advertisement
JayBeeOH

Binary To Decimal Converter (Console App)

Jun 27th, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.11 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2017. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Binary to Decimal Converter
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   27 JUN 2017
  16. '
  17. ' Description:    Visual Basic Console app to convert a binary string into a
  18. '                 decimal value.
  19.  
  20. '                 Documentation is at:
  21. '                   App's Visual Basic .NET code is at https://pastebin.com/J2Ch9cb8
  22. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  23. '-------------------------------------------------------------------------------------------
  24.  
  25. Module MainLIne
  26.  
  27.     Sub Main()
  28.  
  29.         ' Set Console attributes.
  30.         Console.Title = "Binary to Decimal Converter"
  31.  
  32.         Console.ForegroundColor = ConsoleColor.Yellow
  33.         Console.BackgroundColor = ConsoleColor.Blue
  34.         Console.Clear()
  35.  
  36.         ' Prime read
  37.         Dim binaryInput As String = GetValidInput()
  38.  
  39.         Do Until String.IsNullOrWhiteSpace(binaryInput)
  40.             Dim numericValue As Double = 0
  41.             Dim binLength = binaryInput.Length
  42.  
  43.             For idx As Integer = 2 To binLength
  44.                 If binaryInput.Substring(binLength - idx, 1) = "1" Then
  45.                     numericValue += (2 ^ (idx - 1))
  46.                 End If
  47.             Next
  48.  
  49.             If binaryInput.Substring(binLength - 1, 1) = "1" Then
  50.                 numericValue += 1
  51.             End If
  52.  
  53.             Console.WriteLine("Decimal value is: {0}", numericValue.ToString)
  54.             Console.WriteLine()
  55.             binaryInput = GetValidInput()
  56.         Loop
  57.     End Sub
  58.  
  59.     Private Function GetValidInput() As String
  60.         Dim input As String = String.Empty
  61.         Dim validData As Boolean
  62.  
  63.         Do
  64.             validData = True
  65.             Console.WriteLine("Press [Enter] key to exit or ...")
  66.             Console.Write("Enter a binary string to be converted to decimal value: ")
  67.             input = Console.ReadLine()
  68.             For idx As Integer = 0 To input.Length - 1
  69.                 Dim tempString As String = input.Substring(idx, 1)
  70.                 If tempString <> "0" AndAlso tempString <> "1" Then
  71.                     Console.WriteLine("Enter only 0's and 1's. Try again.")
  72.                     Console.WriteLine()
  73.                     validData = False
  74.                     Exit For
  75.                 End If
  76.             Next
  77.         Loop Until validData
  78.         Return input
  79.     End Function
  80. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement