Sign up to the jQuery Grid Subscription list.

Convert ASCII Code/Character to Binary Number

Original post by rattlesnake316 at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2159243&SiteID=1

The 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!

The accepted answer by Martin Xie - MSFT :

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:


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

0 comments:

Related Ads