Skip to content

Instantly share code, notes, and snippets.

@winhtut
Last active March 4, 2019 18:59
Show Gist options
  • Save winhtut/fee6c1044994a64ba2b623ebd6838c96 to your computer and use it in GitHub Desktop.
Save winhtut/fee6c1044994a64ba2b623ebd6838c96 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
//void binary_hex(int n, char hex[]);
long long int hex_binary(char hex[]);
int main()
{
char hex[20] = {'A','A','A'};
char c=0;
int n = 0;
printf("Binary number: %lld", hex_binary(hex));
getch();
return 0;
}
//Function to convert hexadecimal to binary.
long long int hex_binary(char hex[]) {
int i, length, decimal = 0;
long long int 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;
}
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
#define WinHtut 100
int hex_binary(char hex[]);
int result[WinHtut];
int x = 0;
int y = 0;
int main()
{
char hex[20] = {'A','A','A'};
int i, 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);
}
printf("%d\n", decimal);
while (decimal != 0)
{
result[x] =decimal % 2;
decimal = decimal / 2;
x++;
}
printf("The number of Stored in the Array is %d\n", x);
printf("The binary value of the A A A is = ");
for (y = x - 1; y >= 0; y--) {
printf("%d", result[y]);
}
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment