Sign up to the jQuery Grid Subscription list.

Convert Binary To Ascii

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


The Question :

Hi,

I am reading some binary data through my COM Port.

On my form are 2 multi-line text boxes and 14 single line text boxes.

Currently I convert the binary data into hex and display it in one of the multi-line text boxes.

I convert the binary data into hex as each byte is read and then display it.

I would like to also display the binary data into ascii in the other multi-line text box. And also split the whole ascii string into substrings at certain points and display each one in the single line text boxes. I would like to process this all at the same time? How can this be done?

Cheers

Mark



The Accepted Answer (By Martin Xie - MSFT)

Hi Mark,

Spaceman1972 wrote:

Currently I convert the binary data into hex and display it in one of the multi-line text boxes.

I convert the binary data into hex as each byte is read and then display it.

By means of Convert class, you can first convert Binary to Decimal Intger, and then convert Decimal to Hexadecimal string, finally assign it to the TextBox.Text property.

Code Snippet

Imports Microsoft.VisualBasic

Imports System

Imports System.Collections.Generic

Imports System.Text

Dim bin As String = "1000001" 'Binary string

Dim decimalValue As Integer

Dim hexValue As String

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

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

TextBox.Text = hexValue 'Display it in the multi-line textbox



Spaceman1972 wrote:

I would like to also display the binary data into ascii in the other multi-line text box. And also split the whole ascii string into substrings at certain points and display each one in the single line text boxes. I would like to process this all at the same time? How can this be done?

You can first convert Binary data to Decimal Intger (as ASCII Codes), and then convert ASCII Codes to ASCII Characters, finally assign it to the TextBox.Text property.

Code Snippet

Dim bin As String = "1000001" 'Binary string (A's ASCII Code 65)

Dim decimalAscIIValue As Integer

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

Dim AscIIChar As String = Chr(decimalAscIIValue) 'Then convert Decimal to ASCII Character

TextBox.Text = AscIIChar 'AscIIChar =A. Display it in the multi-line textbox

I hope this can enlighten you.

I believe you can finish the rest task.

Best regards,

Martin Xie

0 comments:

Related Ads