binary to hexadecimal in c
#include <math.h>
#include <stdio.h>
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
4.17
6
/*
* C Program to Convert Binary to Hexadecimal
* My Github: https://github.com/krishnan-tech
*/
#include <stdio.h>
int main()
{
long int binaryval, hexadecimalval = 0, i = 1, remainder;
printf("Enter the binary number: ");
scanf("%ld", &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("Equivalent hexadecimal value: %lX", hexadecimalval);
return 0;
}
Thank you!
6
0
3
1
#include <stdio.h>
#include <string.h>
#include <math.h>
int binary_converter(char binary[], int length)
{
int decimal = 0;
int position = 0;
int index = length - 1;
while (index >= 0)
{
decimal = decimal + (binary[index] - 48) * pow(2, position);
index--;
position++;
}
return decimal;
}
int main()
{
printf("\n\t\t\tBINARY TO DECIMAL CONVERTER VIA TERMINAL\n\n\n");
char binary[500];
int decimal = 0;
int length;
printf("\t You have to enter a binary number and we will convert into decimal for you. type 'x' to exit\n");
while (1)
{
printf("BINARY : ");
scanf("%s", binary);
printf("\n");
length = strlen(binary);
for (int i = 0; i < length; i++)
{
if (binary[i] == 'x')
{
printf("\nThanks for using our Converter.\n\n");
return 0;
}
if (binary[i] < 48 || binary[i] > 49)
{
printf("%s is not a BINARY number. \n\n", binary);
break;
}
else
{
if (i == length - 1)
{
decimal = binary_converter(binary, length);
printf("DECIMAL = %d \n\n", decimal);
}
continue;
}
}
}
return 0;
}
Thank you!
1
0
Are there any code examples left?
New code examples in category C
-
C 2022-03-27 22:30:45 Problem Statement Print the following output: \ Input Format IN Output Format \
-
-
-
-
-
-
-