Im trying to figure out why this code only takes in hex numbers with 2 digits. For example, if I enter, "11" it will output, "00010001" but if I enter, "111" then it gives me some random number. I would like to try to make it accept as many digits as the user wants.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
void binary_hex(int n, char hex[]);
int hex_binary(char hex[]);
int main()
{
char hex[20],c;
int n;
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Binary number: %d",hex_binary(hex));
system("pause");
return 0;}
//Function to convert hexadecimal to binary.
int hex_binary(char hex[]) {
int i, length, decimal=0, binary=0;
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
decimal+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
decimal+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
decimal+=(hex[i]-87)*pow(16,length-1);
}
//At this point, variable decimal contains the hexadecimal number in decimal format.
i=1;
while (decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}
return binary;
}
You need to use an array to store the binary number, as an int variable won't be able to store a large number, the range of int is(usually) from −32767 to +32767. C data types.
For example:
#include <stdio.h>
#include <math.h>
int hex_binary(char hex[], int binary_number[]);
int main()
{
char hex[20];
int binary_number[100];
int j,k;
printf("Enter hexadecimal number: ");
scanf("%19s",hex);
j = hex_binary(hex,binary_number);
printf("Binary number is: ");
for(k=j-1;k>=0;k--)
printf("%d",binary_number[k]);
printf("\n");
return 0;
}
//Function to convert hexadecimal to binary.
int hex_binary(char hex[], int binary_number[])
{
int i, j=0, length, decimal=0;
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
decimal+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
decimal+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
decimal+=(hex[i]-87)*pow(16,length-1);
}
//At this point, variable decimal contains the hexadecimal number in decimal format.
while (decimal!=0)
{
binary_number[j++] = decimal%2;
decimal/=2;
}
return j;
}
Output
Enter hexadecimal number: 111
Binary number is: 100010001
Enter hexadecimal number: 1111
Binary number is: 1000100010001
Enter hexadecimal number: 11111
Binary number is: 10001000100010001
Enter hexadecimal number: 987634
Binary number is: 100110000111011000110100
Related
Scan a number along with its base value. If a number starts with 0 it is octal nd if it starts with 0x it is hexadecimal. print the corresponding decimal value.
#include<stdio.h>
int main()
{
int a,b;
scanf("%x %d",&a,&b);
printf("%d",a);`
}
3 test cases shows error .please help me with this.
Use scanf i flag:
Matches an optionally signed integer; the next pointer
must be a pointer to int. The integer is read in base 16
if it begins with 0x or 0X, in base 8 if it begins with 0,
and in base 10 otherwise. Only characters that correspond
to the base are used.
#include<stdio.h>
int main(void)
{
int a,b;
scanf("%i %i",&a,&b);
printf("%d %d\n", a, b);`
return 0;
}
#include<stdio.h>
int main(void)
{
int oc,hex;
scanf("%i",&oc);
scanf("%i",&hex);
printf("\ndecimal:%d",oc);
printf("\ndecimal:%d",hex);
return 0;
}
another way
#include<stdio.h>
int main()
{
int oc,hex;
printf("enter the octal value:");
scanf("%o",&oc);
printf("enter the hex value:");
scanf("%x",&hex);
printf("\noctal to decimal:%d",oc);
printf("\nhex to decimal:%d",hex);
}
i want to convert from binary to decimal but convert function does not return any thing where is the problem ??
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int convert(int x); //fun to convert from binary to decimal
int main()
{
int x; //binary number
printf("plz enter binary number !\n\nbinary: ");
scanf("%d",&x);
printf("\ndecimal: ",convert(x));
return 0;
}
int convert(int x)
{
int sum=0; //decimal number
int i=0;
int r; //remainder
while(x!=0){
r=x%10;
sum+=r*pow(2,i);
x=x/10;
i++;
}
return sum;
}
You can also try
scanf("%i", &x);
printf("%d", x);
you should change your printf in main().
printf("\ndecimal: %d",convert(x));// add %d
Because to print a value by printf need to spacify the data type such as for int %d for character %c.
binary numbers are series of ones and zeros (0|1), if you are referring to this type of numbers then check this source code below. you may also find this link useful http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int bintodec(char bin[32]){
int dec=0;
while(*bin){
if((*bin!='0') && *bin!='1')
break;// only accept '0' & '1'
dec<<=1;
dec+=*bin-'0';
bin++;
}
return dec;
}
int main(){
char bin[32]=""; //i believe binary numbers are series of '1' and '0'
printf("plz enter binary number !\n\nbinary: ");
scanf("%s",bin);
printf("\ndecimal: %d\n",bintodec(bin));
return 0;
}
Use format specifier:
%d for integer (in this case)
%c for character
%f for float value
%s for string
%lf for double
%x for hexadecimal
In this case change print statement to:
printf("\ndecimal: %d",convert(x));
I've tried making a calculator that first reads the input from the user and then decides wheter the input is in binary or in decimal and then converts it.
i got almost everything but can't come up with the identifier! here's the code that i got:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int a;
char num[100];
int number[100];
int decimal_binary (int n);
int binary_decimal (int n);
void errorcode(void);
bool bin, decim;
int main (){
bin (false);
decim (false);
printf ("enter a number: ");
gets (num);
a = strlen(num);
if (a > 8){
errorcode();
//printf("That is an invalid number!");
//exit(45);
}
for (int i=0; i < a; i++){
number[i] = num[i] -'0';
printf("%d", number[i]);
}
int decimal_binary(int n){
int rem, i=1, binary = 0;
while (n != 0)
{
rem =n%2;
n/=2;
binary+=rem *i;
i*=10;
}
return binary;
}
int binary_decimal(int n)
{
int decimal = 0, i=0, rem;
while (n!-0){
rem = n%10;
n/=10;
decimal += rem *pow(2,i);
++i;
}
return decimal;
}
}
void errorcode(void){
printf("That is an invalid number!");
exit(45);
}
There is no such way to detect whether the number entered was binary or decimal just by analysing the characters of the number entered. However, if you study the Microsoft Windows calculator application that ships along with the OS, the "programmer" type of calculator takes an input whether the entered number is binary or decimal from the user, then internally in its code it would check whether the entered number was a valid binary or decimal number.
In short, your code will have to tell the user to specify what number is entered.
I would recommend studying the Microsoft calculator application's "programmer" type calc in order to help you understand what you want to achieve.
I believe the correct answer to this question is that binary numbers can be identified with the 0b prefix. Ex: 0b11111111 is the byte 0xFF, or 255 decimal. Notice the 0x prefix on the hex number. Decimal numbers have no prefix.. In fact, if you lead your 'decimal' number with a zero, the compiler will think that it is an octal number and treat it as base 8.
I wanted to print a number to variable number of decimal places in C.
I have written the code
#include<stdio.h>
main()
{ int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.af", a, 2.554648);
return 0;
}
Use * in printf() to mark how many decimal places you want:
#include <stdio.h>
int main(void)
{
int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.*f", a, a, 2.554648);
return 0;
}
You need the * format specifier. Here is a short example(see in ideone):
#include <stdio.h>
int main(void) {
int a = 5;
double temp = 5.0 / 7;
printf("%.*f",a, temp);
return 0;
}
The program is just a base 10 to base 2 converter it works fine. I just want the cursor at the new line.
I have put "\n" everywhere I can logically put it but it just does not bring the cursor to the new line. The program is doing what I need it to do.
EX
"Enter a decimal number from 0 to 18,446, 744,073,709,551,615: 156"
The binary value for 156 is:10011100{{CURSOR}}
(I want the {{CURSOR}} here)
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
return 0;
}
When all is said and done, you need to print a newline after the number is done using printf("\n"). The rest, like you said, does what it should:
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
printf("\n");
return 0;
}