Shetty
Shetty

Reputation: 19

How to represent binary data in 8 bits in C

#include<stdio.h>

int main()
{
    long int decimalNumber,remainder,quotient;
    int binaryNumber[100],i=1,j;

    printf("Enter any decimal number: ");
    scanf("%ld",&decimalNumber);
    quotient = decimalNumber;

    while(quotient!=0)
    {
        binaryNumber[i++]= quotient % 2;
        quotient = quotient / 2;
    }

    printf("Equivalent binary value of decimal number %d: ",decimalNumber);

    for(j = i -1 ;j> 0;j--)
        printf("%d",binaryNumber[j]);

    return 0;
}

I want the output in 8 bit binary form, but the result as shown below, is there any operator in C which can convert 7 bit data to its equivalent 8 bit data? thank you

Sample output:

Enter any decimal number: 50

Equivalent binary value of decimal number 50: 110010

Required output is 00110010 which is 8 bit, how to append a zero in MSB position?

Upvotes: 1

Views: 6238

Answers (3)

DrKoch
DrKoch

Reputation: 9772

Use this for printing your result:

for(j = 7; j>0; j--)
    printf("%d", binaryNumber[j]);

This always prints 8 binary digits.


Edit

The int array binaryNumber must be initialized with zeros to make this work:

for(int i=0; i<8; i++) binaryNumber[i] = 0;

Upvotes: 1

Mohit Jain
Mohit Jain

Reputation: 30489

Modify your code as shown below:

  quotient = quotient / 2;
}
/* ---- Add the following code ---- */
{
  int group_size = 8;  /* Or CHAR_BIT */
  int padding = group_size  - ((i-1) % group_size); /* i was inited with 1 */
  if(padding != group_size) {
     /* Add padding */
     while(padding-- != 0) binaryNumber[i++] = 0;
  }
}
/* ------- Modification ends -------- */
printf("Equivalent binary value of decimal number %d: ",decimalNumber);

This code calculates the number of padding bits required to print the number and fills the padding bits with 0.

Live demo here

If you want 7 bit answer, change group_size to 7.

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84569

A very convenient way it so have a function return a binary representation in the form of a string. This allows the binary representation to be used within a normal printf format string rather than having the bits spit out at the current cursor position. To specify the exact number of digits, you must pad the binary string to the required number of places (e.g. 8, 16, 32...). The following makes use of a static variable to allow the return of the buffer, but the same can easily be implemented by allocating space for the buffer with dynamically. The preprocessor checks are not required as you can simply hardwire the length of the buffer to 64 + 1, but for the sake of completeness a check for x86/x86_64 is included and BITS_PER_LONG is set accordingly.

#include <stdio.h>

#if defined(__LP64__) || defined(_LP64)
# define BUILD_64   1
#endif

#ifdef BUILD_64
# define BITS_PER_LONG 64
#else
# define BITS_PER_LONG 32
#endif

char *binstr (unsigned long n, size_t sz);

int main (void) {

    printf ("\n 50 (decimal)  :  %s (binary)\n\n", binstr (50, 8));

    return 0;
}

/* returns pointer to binary representation of 'n' zero padded to 'sz'. */
char *binstr (unsigned long n, size_t sz)
{
    static char s[BITS_PER_LONG + 1] = {0};
    char *p = s + BITS_PER_LONG;
    register size_t i;

    if (!n) {
        *s = '0';
        return s;
    }

    for (i = 0; i < sz; i++)
        *(--p) = (n>>i & 1) ? '1' : '0';

    return p;
}

Output

$ ./bin/bincnv

 50 (decimal)  :  00110010 (binary)

Note: you cannot make repeated calls in the same printf statement due to the static buffer. If you allocate dynamically, you can call the function as many times as you like in the same printf statement.

Also, note, if you do not care about padding the binary return to any specific length and just want the binary representation to start with the most significant bit, the following simpler version can be used:

/* simple return of binary string */
char *binstr (unsigned long n)
{
    static char s[BITS_PER_LONG + 1] = {0};
    char *p = s + BITS_PER_LONG;

    if (!n) {
        *s = '0';
        return s;
    }

    while (n) {
        *(--p) = (n & 1) ? '1' : '0';
        n >>= 1;
    }
    return p;
}

Upvotes: 3

Related Questions