?
Solved

ASCII to Binary

Posted on 2006-07-09
7
Medium Priority
?
1,541 Views
Last Modified: 2013-11-15
Hi there,I'm a novice programmer taking an online C course. ive come up against a problem in an exercise that Im hoping to get help with.

Im writing a function to convert a string of ascii chars to binary.

So far what I have is this

void asciiBinary (void)
      {
       char* bb = "3D";

               //looping variables
                int i;
       int y;

      for (i =0;i < strlen(bb); i++)
            {
            char x =bb[i];
            for(y = 0; y < sizeof(char) * 8; y++)  
                  {
                   printf("%c ", ( x & (1 << y) ) ? '1' : '0' );
                  }
            printf("  ");
            }
      }

I've heavily adapted this from code used in another binary function.

Now this is cool, it almost works, but it prints out the binary numbers in reverse

ie     11001100  00100010
not   00110011  01000100

I think the fault lies in the bitwise operator...

Any advice on how to fix this?

Thanks
Benjamin
0
Comment
Question by:Benjamin_Barrett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 1000 total points
ID: 17068540
Hi Benjamin_Barrett,

You're not only converting ASCII to binary, you're converting HEX ASCII to binary.  It's important to know what base arithmetic must be performed.  :)

The good news is that it's really quite easy once you get the hang of it.

Consider a base 10 number.  You and I see the character '0' as "nothing", but the computer "sees" a string of bits that display as a '0'.  You and I see a '1' a the number one, but the computer "sees" a string of bits that display as a '1'.  Same for the other digits.

To convert the number from ASCII to binary, you simply subtract the code for '0'.  Since the characters '0', '1', '2', '3', etc are consective, you wind up with the binary form of the digit.

With hex conversions you have potentially two steps for each digit.  If the digit is '0' to '9' you simply subtract '0' (the ASCII code for 0).  If it's 'a' to 'f' (or 'A' to 'F'), you subtract the ASCII code for 'a' (or 'A') and add 10.  You add 10 because 'a' (or 'A') represents 10 decimal and subtracting 'a' (or 'A') reduces to zero.

Programmatically, you convert a digit like this:

   if (isdigit(ASCIIChar))
     BinaryDigit = ASCIIChar  - '0';
   else
     BinaryDigit = tolower(ASCIIChar) - 'a' + 10;

Written on one line, it looks like this:

   BinaryDigit = (isdigit(ASCIIChar) ? ASCIIChar - 10 : tolower(ASCIIChar) - 'a' + 10;


Note that for this kind of arithmetic ASCIIChar is an unsigned char.


Good Luck!
Kent
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1000 total points
ID: 17068662
Hi Benjamin,

Take a look at this bit:

( x & (1 << y) ) ? '1' : '0'

This is testing a particular bit of x to see if it is a 1 or a 0. To pick a different bit you will need to change (1<<y) to something else. To get the bits in the opposite order try something like (1 << (7-y)) but please try to understand why. Study the meaning of the line above.

Paul
0
 

Author Comment

by:Benjamin_Barrett
ID: 17068672
Hi Kent

Thanks for the quick reply and your excellent explanation. You have made it much clearer :)

Tried incorporating your suggestion into my code but Im still having problems....

void asciiBinary (void)
      {
       char* bb = "D3";
      char BinaryDigit;

      int i;
      int y;
      for (i =0;i < strlen(bb); i++)
            {

            char x =bb[i];

               if (isdigit(x))
                  {
                  BinaryDigit = x  - '0';
                  }
                  else
                  {
            BinaryDigit = tolower(x) - 'a' + 10;
                  }
            
            for(y = 0; y < sizeof(char) * 8; y++)  
                  {
                  char prnt = (isdigit(BinaryDigit) ? BinaryDigit - 10 : tolower(BinaryDigit) - 'a' + 10);

                   printf("%c ", prnt);
                  }
            printf("\n");
            }
      }
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:Benjamin_Barrett
ID: 17068683
Sorry didnt finish before submitted....

the output is 8 pi symbols!

Any ideas?
0
 

Author Comment

by:Benjamin_Barrett
ID: 17068701
Success!

Paul, my first attempt at understanding would be:

The  binary respresentation is a byte or 8 bits

To move thru the bits using the looping indice (y) and print them out the other way I'd need to subtract the indice from the the length of the byte.

But why is this not (8 - y) then?

or is a byte accessed like an array ie from 0

What do you think?

0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17068730
>>But why is this not (8 - y) then?
Because the top bit is 1<<7.

1<<0 == 0000 0001
1<<1 == 0000 0010
1<<2 == 0000 0100
1<<3 == 0000 1000
1<<4 == 0001 0000
1<<5 == 0010 0000
1<<6 == 0100 0000
1<<7 == 1000 0000

The first time around the loop, y == 0.

Paul
0
 

Author Comment

by:Benjamin_Barrett
ID: 17071051
Ah

Great, thanks for your help guys
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
Invest in your employees with these five simple steps to improve employee engagement and retention.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question