#include <stdio.h>
int main(int argc, char *argv[]){
char c;
int i,dec_val;
printf(" Give a binary number with length 16 : \n");
i=15;
dec_val=0;
while (i>=0) {
dec_val+= (getchar()-'0')*(1 << i);
i=i-1;
}
printf ("%d \n",dec_val);
return(0);
}
As you probably noticed no check is made. User inputs 16 chars which must be 0 and 1. After that characters are read and expected to be 0-1 and multiplied by a power of two.
Also this post here is a decimal to binary converter.
No comments:
Post a Comment