locked
How to convert ASCII Code/Character to Binary Number?

    Question

  •  

    Hello! Hope someone could help me on converting ASCII characters to Binary


    Also,hope you know how to convert Boolean Number to Octal or Hexadecimal or Decimal number.

    Thanks in advance!

    Saturday, September 15, 2007 12:34 AM

Answers

  •  rattlesnake316 wrote:

    Hello! Hope someone could help me on converting ASCII characters to Binary.

     

    Hi rattlesnake,

     

    ASCII can only represent character codes between 0 and 127.

     

    About converting ASCII characters to Binary, 

    e.g. convert AscII value 66 of character B to Binary like this:

    Code Snippet

    Imports Microsoft.VisualBasic

    Imports System

    Imports System.Collections.Generic

    Imports System.Text

     

              Friend Class Program

              Shared Sub Main(ByVal args As String())

             

                Dim myChar As Char = "B"c

                Dim j As Integer = 0

                Dim ascii As Integer = System.Convert.ToInt32(myChar)

                Console.WriteLine("ASCII:" & ascii)

                Dim binTempResult As String = ""

                     Do While ascii > 0

                           j = ascii Mod 2

                           binTempResult &= j.ToString()

                           ascii = ascii \ 2

                     Loop

     

                'reverse output

                Dim arr As Char() = binTempResult.ToCharArray()

                Array.Reverse(arr)

                Dim binResult As String = New String(arr)

                System.Console.Write(binResult)

                Console.Read()

     

              End Sub

     

              End Class

     

    The output will be:

    ASCII:66

    1000010

    Tuesday, September 18, 2007 3:29 AM
  •  rattlesnake316 wrote:

    Sorry for my error, the real question is how to convert binary to ocatl,hexadecimal and decimal numbers...

     

    By means of Convert class,  you can first convert Binary to Decimal Intger, and then convert Decimal to Octal and Hexadecimal string.

    Code Snippet

    Imports Microsoft.VisualBasic

    Imports System

    Imports System.Collections.Generic

    Imports System.Text

     

              Friend Class Program

                       Shared Sub Main(ByVal args As String())

             

            Dim bin As String = "1000001"    'Binary string

     

            Dim decimalValue As Integer

            Dim hexValue As String

            Dim OctalValue As String

     

            decimalValue = Convert.ToInt32(bin, 2)   'First convert Binary to Decimal Intger

            Console.WriteLine("Decimal:" & decimalValue)

     

            OctalValue = Oct(decimalValue)     'Convert Decimal to Octal string

            Console.WriteLine("Octal:" & OctalValue)

     

            hexValue = Hex(decimalValue)       'Convert Decimal to Hexadecimal string

            Console.WriteLine("Hexadecimal:" & hexValue)

     

            Console.Read()

     

                       End Sub

     

              End Class

     

    The output will be:

    Decimal: 65

    Octal: 101

    Hexadecimal: 41

    Regards,

    Martin

    Wednesday, September 19, 2007 2:24 AM
  • All replies

    • This looks like a simple question, but it's a little confusing. Ascii characters are binary - they just happen to be an agreed upon representation of written characters.

       

      My question is... what kind of conversion are you looking for? What are you trying to accomplish?

      Monday, September 17, 2007 5:49 PM
    •  rattlesnake316 wrote:

      Hello! Hope someone could help me on converting ASCII characters to Binary.

       

      Hi rattlesnake,

       

      ASCII can only represent character codes between 0 and 127.

       

      About converting ASCII characters to Binary, 

      e.g. convert AscII value 66 of character B to Binary like this:

      Code Snippet

      Imports Microsoft.VisualBasic

      Imports System

      Imports System.Collections.Generic

      Imports System.Text

       

                Friend Class Program

                Shared Sub Main(ByVal args As String())

               

                  Dim myChar As Char = "B"c

                  Dim j As Integer = 0

                  Dim ascii As Integer = System.Convert.ToInt32(myChar)

                  Console.WriteLine("ASCII:" & ascii)

                  Dim binTempResult As String = ""

                       Do While ascii > 0

                             j = ascii Mod 2

                             binTempResult &= j.ToString()

                             ascii = ascii \ 2

                       Loop

       

                  'reverse output

                  Dim arr As Char() = binTempResult.ToCharArray()

                  Array.Reverse(arr)

                  Dim binResult As String = New String(arr)

                  System.Console.Write(binResult)

                  Console.Read()

       

                End Sub

       

                End Class

       

      The output will be:

      ASCII:66

      1000010

      Tuesday, September 18, 2007 3:29 AM
  •  rattlesnake316 wrote:

    Also,hope you know how to convert Boolean Number to Octal or Hexadecimal or Decimal number.

     

    Boolean value is either True or False.

    You only need to conver True to 1 and False to 0.

    The Octal or Hexadecimal or Decimal numbers of the 1/0 are always 1/0.

     

    If we misunderstood you, please let me know.

    Tuesday, September 18, 2007 3:41 AM
  •  

    Martin,

     

    This is not necessarily true across all Microsoft languages. Please see System.ConvertoBoolean(Object)

     

    Although the OP's questions aren't particulary clear...well...who knows how much depth is behind them.

     

    Please have a good evening.

    Tuesday, September 18, 2007 4:26 AM
  • Hi Renee,

    Thank you again!

    I'm in China, now it's daytime. Have a good evening to you.

     

     

    Hi rattlesnake, if you still have any doubt and concern about this case, please clarify your questions.

     

    Thanks,

    Martin

     

    Tuesday, September 18, 2007 7:33 AM
  • Sorry for my error, the real question is how to convert binary to ocatl,hexadecimal and decimal numbers...

     

    Thanks!

     

    Wednesday, September 19, 2007 12:56 AM
  •  rattlesnake316 wrote:

    Sorry for my error, the real question is how to convert binary to ocatl,hexadecimal and decimal numbers...

     

    By means of Convert class,  you can first convert Binary to Decimal Intger, and then convert Decimal to Octal and Hexadecimal string.

    Code Snippet

    Imports Microsoft.VisualBasic

    Imports System

    Imports System.Collections.Generic

    Imports System.Text

     

              Friend Class Program

                       Shared Sub Main(ByVal args As String())

             

            Dim bin As String = "1000001"    'Binary string

     

            Dim decimalValue As Integer

            Dim hexValue As String

            Dim OctalValue As String

     

            decimalValue = Convert.ToInt32(bin, 2)   'First convert Binary to Decimal Intger

            Console.WriteLine("Decimal:" & decimalValue)

     

            OctalValue = Oct(decimalValue)     'Convert Decimal to Octal string

            Console.WriteLine("Octal:" & OctalValue)

     

            hexValue = Hex(decimalValue)       'Convert Decimal to Hexadecimal string

            Console.WriteLine("Hexadecimal:" & hexValue)

     

            Console.Read()

     

                       End Sub

     

              End Class

     

    The output will be:

    Decimal: 65

    Octal: 101

    Hexadecimal: 41

    Regards,

    Martin

    Wednesday, September 19, 2007 2:24 AM