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

Join 117,577 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 2,129 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



ASCII to binary

 
Reply to this topicStart new topic

ASCII to binary

PsychoCoder
post 16 Jul, 2008 - 05:38 PM
Post #1


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,831



Thanked 80 times

Dream Kudos: 8050

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

My Contributions


Well I'm trying to figure out what I'm missing here. I'm attempting to convert an ASCII string to its binary equivilent, but Im not quite getting the right results (it's always just a bit or two off), in fact sometimes it almost looks backwards


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);

//convert to binary
try
{
writer.Write(byteArray);
}
catch (Exception ex)
{
return ex.Message;
}


BitArray bitArray = new BitArray(stream.ToArray());

for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
converted += 1;
}
else
{
converted += 0;
}
}
return converted;
}


GetByteArray is a simple one line method


csharp

private byte[] GetByteArray(string str)
{
return Encoding.ASCII.GetBytes(str);
}
User is online!Profile CardPM

Go to the top of the page


born2c0de
post 17 Jul, 2008 - 01:56 AM
Post #2


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,812



Thanked 27 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


QUOTE
in fact sometimes it almost looks backwards
In fact it's always backwards. The conversion is perfect (bit-to-bit)

As far as the reversing goes, you are storing it in reverse order.

In the BitArray Class the lowest index value (i.e zero) refers to the least significant bit of the number stored.

Let us assume that the number 32 is stored in a BitArray object.
So when storing 32 (00000100), index 2 will refer to 1 and not 0.

You are reading from 0 to bitarray.Length (i.e. from LSB to MSB) and concatenating each element in a string, which in turn reverses the result.
Hence the final string is stored like this:
[LSB], [LSB+1], [LSB+2], ..., [MSB]

Iterate from MSB to LSB and problem solved!
smile.gif
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 17 Jul, 2008 - 04:43 AM
Post #3


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,763



Thanked 70 times

Dream Kudos: 400

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


As stated, it's always backwards. Or forwards, depending on how you look at it.

If you think about it, we read decimal numbers backwards so often we don't even think about it. e.g. the number 456 is 4*100 + 5*10 + 6*1. In order to decipher normal numbers, we have to mentally take in the whole thing first before we can recon it's size. How much more efficient if the first digit was the ones place... Like, well, a computer.

Here's some code:

csharp

string ByteToBitStringNaturalOrder(byte value) {
string s = "";
for (int i = 0; i < 8; i++) {
s += (value & 1);
value >>= 1;
}
return s;
}

// ByteToBitStringNaturalOrder(5) yeilds 10100000, "backwards"

string ByteToBitString(byte value) {
string s = "";
for (int i = 0; i < 8; i++) {
s += (value & 0x80)>0 ? "1" : "0";
value <<= 1;
}
return s;
}

//ByteToBitString(5) yeilds 00000101


There are a number of ways to get it in the order you want, but with only eight bits, I like the one I used.

For completelness, here's how I'd do an array:

csharp

string ByteToBitString(byte[] values) {
string s = "";
for(int i=values.Length-1; i>=0; i--) {
s += ByteToBitString(values[i]);
}
return s;
}


I'm actually writing an arbitrary size number class in C++ right now so all the bit manip stuff is pretty fresh in the brain.

Hope this helps and that I didn't ruin the fun of doing it yourself.
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 17 Jul, 2008 - 02:09 PM
Post #4


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,831



Thanked 80 times

Dream Kudos: 8050

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

My Contributions


Thanks, works perfectly now! This is the final code:


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);

//convert to binary
try
{
writer.Write(byteArray);
}
catch (Exception ex)
{
return ex.Message;
}

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 online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 08:54PM

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