Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,116 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,997 people online right now. Registration is fast and FREE... Join Now!




Binary to ASCII conversion

 
Reply to this topicStart new topic

Binary to ASCII conversion

nomeealy
post 10 Oct, 2008 - 12:17 AM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 3

CODE

                    string[] arrStrings = richTextBox2.Text.Trim(' ').Split(' ');
                    string sText = "";
                    foreach (string st in arrStrings)
                    sText += Convert.ToChar(Convert.ToInt32(st, 2));
                    richTextBox3.Text = sText;



This is the code of converting binary data in ASCII but it can convert only 16 bits of binary data to ascii , after that it throws an exception. I have got a data(payload) of about 200 bits + so how can I manipulate this code to get the conversion. Please help me out , its urgent !!

NoMee ALy
User is offlineProfile CardPM

Go to the top of the page

ragingben
post 10 Oct, 2008 - 06:34 AM
Post #2


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 30


My Contributions


Hi nomeealy, I have created a small sample project with the 2 rich text boxes, and tested your code, it all works fine. I used a 8 bit binary string for the character "j" and copy and pased it into the text box many times (seperated by the space to allow the split), possibly as much as 200 times, and the code converted it fine each time.

Can you be more specific with the exception that is thrown?
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 10 Oct, 2008 - 07:36 AM
Post #3


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,914



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Here's a snippet I wrote that converts ASCII to it's binary form, this may help you solve your issues:

csharp

public string ToBinary(string str)
{
string converted = string.Empty;
byte[] byteArray = GetByteArray(str);

//create a memory stream
MemoryStream stream = new MemoryStream();

//create BinaryWriter based on our MemoryStream
BinaryWriter writer = new BinaryWriter(stream);

try
{
//write out in binary
writer.Write(byteArray);
}
catch (Exception ex)
{
return ex.Message;
}

//now we get to start the conversion: Loop through each
//byte in the byte array
for (int i = 0; i < byteArray.Length; i++)
{
for (int j = 0; j < 8; j++)
{
converted += (byteArray[i] & 0x80) > 0 ? "1" : "0";
byteArray[i] <<= 1;
}
}

return converted;
}
User is offlineProfile CardPM

Go to the top of the page

nomeealy
post 13 Oct, 2008 - 02:34 AM
Post #4


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 3

hey raggingben , the program run fine for 8 bit and 16 bit binary , but when u give a 17th bit it will throw an exception .. I have got a 200 bits binary data and want to convert each 16 bits into one ASCII character. I want to breakdown the total bits into 16bits tag like this !

200 bits
16bits--16 bits-- 16 bits-- 16 bits and so on

Please help me out !
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 10:21AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month