locked
How to make Binary converter (text to binary converter) RRS feed

  • Question

  • Hello, how can i make text to binary converter in visual basic Console application, i searched for it on the the internet and i didn't find anything useful for the console application. hope i can find the answer bellow.

    thanks

    Ghazi Mour

    :)

    Thursday, February 11, 2016 5:38 PM

Answers

  • If it is the text itself you want to show in Binary, have a look at this

    Module Module1
    
        Sub Main()
            Dim number As Integer
            Dim input As String = "The quick brown fox jumps over the lazy dog"
            For Each ch As Char In input
                number = Asc(ch)
                Console.WriteLine(ch & " Binary " & _
                                  Convert.ToString(number, 2).PadLeft(8, "0"c) & _
                                  " Decimal " & number.ToString.PadLeft(3, " "c) & _
                                  " Hex 0x" & number.ToString("X2"))
            Next
            Console.ReadLine()
        End Sub
    
    End Module
    

    • Marked as answer by GhaziSM Saturday, February 13, 2016 5:54 AM
    Friday, February 12, 2016 6:45 AM
  • but how can i make a hex converter for the numbers???

    Do you mean that you want to display a number (such as an integer) in hex?

    The VB namespace supports the old HEX function, which accepts a number or a string as the argument.

    https://msdn.microsoft.com/en-us/library/963zt96e%28v=vs.90%29.aspx

    The current version is a standard format specifier ('X' or 'x') that can be used with the Integer.ToString method.  

    https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#XFormatString 

    If you are talking about some other form of conversion, provide an example of what you want to convert and what you want to convert it to.

    • Marked as answer by GhaziSM Saturday, February 13, 2016 5:54 AM
    Friday, February 12, 2016 7:14 AM

All replies

  • Can you give the example of the text you want to convert and explain exactly what you mean by "binary" (everything in a computer is in binary).
    Thursday, February 11, 2016 5:52 PM
  • If the text that you want to convert is a number entered as a string (e.g. "21") and the "binary" that you want to convert it to is a string containing the binary representation of that number (e.g. "10101"), you can do that in a console application like this.

    Sub Main()
        Dim number As Integer
        Dim valid As Boolean
        Do Until valid
            Console.WriteLine("Enter a number")
            Dim input As String = Console.ReadLine
            If Integer.TryParse(input, number) Then
                Console.WriteLine(number.ToString & " in binary is " & Convert.ToString(number, 2))
                valid = True
            Else
                Console.WriteLine("Not a number")
            End If
        Loop
        Console.ReadLine()
    End Sub

    • Proposed as answer by Frank L. Smith Thursday, February 11, 2016 8:01 PM
    Thursday, February 11, 2016 6:13 PM
  • Thank you M.r Smith for this answer, but i want a text & number converter (i can change the text/numbers to binary)

    but how can i make a hex converter for the numbers???


    • Edited by GhaziSM Friday, February 12, 2016 6:35 AM
    Friday, February 12, 2016 6:35 AM
  • If it is the text itself you want to show in Binary, have a look at this

    Module Module1
    
        Sub Main()
            Dim number As Integer
            Dim input As String = "The quick brown fox jumps over the lazy dog"
            For Each ch As Char In input
                number = Asc(ch)
                Console.WriteLine(ch & " Binary " & _
                                  Convert.ToString(number, 2).PadLeft(8, "0"c) & _
                                  " Decimal " & number.ToString.PadLeft(3, " "c) & _
                                  " Hex 0x" & number.ToString("X2"))
            Next
            Console.ReadLine()
        End Sub
    
    End Module
    

    • Marked as answer by GhaziSM Saturday, February 13, 2016 5:54 AM
    Friday, February 12, 2016 6:45 AM
  • but how can i make a hex converter for the numbers???

    Do you mean that you want to display a number (such as an integer) in hex?

    The VB namespace supports the old HEX function, which accepts a number or a string as the argument.

    https://msdn.microsoft.com/en-us/library/963zt96e%28v=vs.90%29.aspx

    The current version is a standard format specifier ('X' or 'x') that can be used with the Integer.ToString method.  

    https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#XFormatString 

    If you are talking about some other form of conversion, provide an example of what you want to convert and what you want to convert it to.

    • Marked as answer by GhaziSM Saturday, February 13, 2016 5:54 AM
    Friday, February 12, 2016 7:14 AM
  • Thank you M.r Smith for this answer, but i want a text & number converter (i can change the text/numbers to binary)

    but how can i make a hex converter for the numbers???


    Please explain clearly what you want. Providing some examples of input and output would help. You didn't mention hex before.
    Friday, February 12, 2016 12:43 PM