0

I am new to C and for an assignment I have to print the 8-bit binary representation of a combination of up to 5 ASCII characters ie. D3% = 01000100 00110011 00100101.

Here is my code:

void ascii_to_binary(int * option_stats, char * ascii)
{
   int i, j, num_value, count, binary[40], length, space=0; /* binary array length of 40 as */
   length = strlen(ascii);                         /*  5*8 bit characters is 40 */ 
   count = 8;
   pos = 0
   printf("Binary representation: ");
   for (i = 0; i < length; i++)
   {
      num_value = ascii[i];

      while(count > 0)
      {
         if((num_value%2) == 0)
         {
            binary[pos] = 0;
            num_value = num_value/2;
            count--;
            pos++;
         }
         else
         {
            binary[pos] = 1;
            num_value = num_value/2;
            count--;
            pos++;
         } 
      }
      count = 8; /*resets loop counter*/
   }
   for(j = pos-1; j >= 0; j--)
   {
      printf("%d", binary[j]);
      space++;
      if(space == 8)
      {
         printf(" "); /*white space between bytes*/
      }
   } 
   read_rest_of_line(); /*used as part of the larger program as a whole, 
                          please ignore*/
}

The ASCII characters I have entered are passed from a separate function, the code is below:

void run_ascii_binary(void)
{
   char input[MAX_STRING_INPUT + EXTRA_SPACES], ascii;
   int option_stats;
   printf("ASCII to Binary Generator\n");
   printf("-------------------------\n");
   printf("Enter a String (1-5 characters): ");
   if (fgets(input, MAX_STRING_INPUT+EXTRA_SPACES, stdin) !=  NULL)
   {
      sscanf(input, "%s", &ascii);
   }
   ascii_to_binary(&option_stats, &ascii);
}

My issue is when it comes to printing the actual binary.

The output I get is: 00100101 11001100 01000100 in which the 1st byte and 3rd byte are in the wrong order. Any tips to get it to print it in the correct order would be great!

thanks!

Lukaaaaaaaay
  • 141
  • 1
  • 4
  • 14

2 Answers2

2

ascii needs to be big enough for the 5 char and a \0.

char ascii[5+1];
...
sscanf(input, "%5s", &ascii);

Initialize option_stats

int option_stats = 0;

Uncomment length = strlen(ascii);. It's being commented out by previous line's unterminated comment.

@LtWorf is correct that OP should loop exactly 8 times.

// while(num_value > 0) {
int bit;
int counti = count;  /// see below
for (i=8; i-- > 0; ) {

Further, the bits are accumulated from least significant to greatness, yet your want to display them greatest to least. So accumulated them in your string in reverse order.

// In two places
// binary[count] = ...
binary[counti] = ...

There are other ways to clean up the code, but these are the 5 main issues.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • thanks for the comment chux. I forgot to mention I haven't implemented the functionality which uses the option_stats variable, that will be done once I've got this working. Thanks for picking up the comment too, I added that last minute when posting the code here, I didn't notice it was commented incorrectly. I've got the loop counting properly, I just need to have them accumulate in the the reverse order. Thanks for the hints, I'll take another look. – Lukaaaaaaaay Sep 16 '13 at 02:10
  • +1 for reading the code and actually *seeing* the problem with `char ascii;` – WhozCraig Sep 16 '13 at 04:05
  • @user1583932 Recommend `fgets(input, sizeof(input), stdin)` instead of `fgets(input, MAX_STRING_INPUT+EXTRA_SPACES, stdin)`. Not germane to this question, but IMHO a better coding practice. – chux - Reinstate Monica Sep 16 '13 at 04:11
  • thanks for the help guys. I have edited my code to the most current version. I am still stuck. I have the output printing the individual bits in the correct order by the bytes are backwards. EG. D3% is entered, my output: 00100101 00110011 01000100 while the correct output is: 01000100 00110011 00100101. Any ideas? – Lukaaaaaaaay Sep 16 '13 at 05:44
  • PS. I haven't updated the ascii[5+1] section as of yet. I want to get it printing right first. – Lukaaaaaaaay Sep 16 '13 at 05:54
  • @user1583932 Insure `ascii[6]` first. Change `for(j = pos-1; j >= 0; j--)` to `for(j = 0; j < pos; j++)` – chux - Reinstate Monica Sep 16 '13 at 14:10
1
while(num_value > 0)

This is wrong, a char can also be negative. You'd better just iterate 8 times instead.

LtWorf
  • 7,286
  • 6
  • 31
  • 45
  • well sizeof(char)*8 should do, but I've never heard of an architecture where that isn't 8. – LtWorf Sep 16 '13 at 05:57
  • 2
    [What platforms have something other than 8-bit char?](http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char) – RedX Sep 16 '13 at 06:04