Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > How to convert ASCII Code/Character to Binary Number?
 

How to convert ASCII Code/Character to Binary Number?

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!

rattlesnake316  Saturday, September 15, 2007 12:34 AM
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 characterBto 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

Martin Xie - MSFT  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 firstconvert 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)'Firstconvert Binary to Decimal Intger

Console.WriteLine("Decimal:" & decimalValue)

OctalValue = Oct(decimalValue) 'Convert Decimal to Octalstring

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

Martin Xie - MSFT  Wednesday, September 19, 2007 2:24 AM

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?

ReneeC  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 characterBto 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

Martin Xie - MSFT  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 arealways 1/0.

If we misunderstood you, please let me know.

Martin Xie - MSFT  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.

ReneeC  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

Martin Xie - MSFT  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!

rattlesnake316  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 firstconvert 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)'Firstconvert Binary to Decimal Intger

Console.WriteLine("Decimal:" & decimalValue)

OctalValue = Oct(decimalValue) 'Convert Decimal to Octalstring

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

Martin Xie - MSFT  Wednesday, September 19, 2007 2:24 AM

You can use google to search for other answers

Custom Search

More Threads

• Is this a bug?
• Splashscreen Question/Problem.
• User alert!
• character and number check
• Update Error
• winform size saved
• Finding out if a textbox contains characters only or integers only
• I can't register
• Remote Computing
• Synchronizing two datagridview