How to Convert Number in Binary Format in C with Example Code



#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"

const char *bit_rep[16] = 

{
"0000", "0001", "0010", "0011", 

"0100", "0101", "0110", "0111", 
"1000", "1001", "1010", "1011", 
"1100", "1101", "1110", "1111"
};


int _tmain(int argc, _TCHAR* argv[])
{
    unsigned long int num = 0x12345000;
    int printnum = num;

    int d = ((sizeof(num)*2)-1);
    printf("Number in Hex : %x\n", num);
    printf("Total Number of bits : %d\n",d);
    printf("Number in bin = ");

    for(d;d>0;d--)
    {      
        int dm = d*4;
        //printf("\n%d\n", (printnum >> dm) & 0x0F);      
        printf("%s", bit_rep[(printnum >> dm) & 0x0F]);      
    }
    printf("%s", bit_rep[printnum & 0x0F]);  
    printf("\n");  

    getch();

    return 0;
}
Share on Google Plus

About Unknown

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...