Greetings EveryOne
I want to present you my Hexadecimal/Decimal converter for feedback.
HexToDec.c:
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int StrMaxSize = 20;
char *HexString = malloc(StrMaxSize);
int *HexNumbAt = malloc(StrMaxSize);
char ValidHexNumber[16] = {'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e',
'f'};
int CurrentStrSize = StrMaxSize-2;
int DecNumb = 0;
int NotValidHexValue = 0;
unsigned int Cnt = 0;
int i = 0;
int k = 0;
printf("Please Enter Your Hexadecimal Number: ");
if(HexString != NULL && HexNumbAt != NULL)
{
int Char = EOF;
while((Char = getchar()) != '\n' && Char != EOF)
{
// Input Hexadecimal Number;
HexString[Cnt] = (char) Char;
Cnt++;
if(Cnt == CurrentStrSize)
{
CurrentStrSize = (Cnt+StrMaxSize)-2;
HexString = realloc(HexString, CurrentStrSize);
HexNumbAt = realloc(HexNumbAt, CurrentStrSize);
}
}
HexString[Cnt] = '\0';
}
// Display the entered Hexadecimal Number;
printf("HexNumber = %s\n", HexString);
// Check for the validity of the entred Hexadecimal Number;
for(i = 0; i < Cnt; i++)
{
char LowerCaseChar = tolower(HexString[i]);
int j = 0;
for(j = 0; j < 16; j++)
{
if(LowerCaseChar == ValidHexNumber[j])
{
break;
}
if(j == 15)
{
NotValidHexValue = 1;
}
}
if(NotValidHexValue)
{
printf("Error: Unrecognized Value\n");
NotValidHexValue = 0;
return -1; // exit program with (-1);
}
}
// Convert Hex to Dec;
for(i=Cnt-1; i >= 0; i--)
{
int x = 0;
if(isdigit(HexString[i]))
{
char Ch[2] = {0};
Ch[0] = HexString[i];
Ch[1] = '\0';
x = atoi(Ch);
HexNumbAt[i] = (x*pow(16,k));
}
if(isalpha(HexString[i]))
{
switch(tolower(HexString[i]))
{
case 'a':
x = 10;
HexNumbAt[i] = (x*pow(16,k));
break;
case 'b':
x = 11;
HexNumbAt[i] = (x*pow(16,k));
break;
case 'c':
x = 12;
HexNumbAt[i] = (x*pow(16,k));
break;
case 'd':
x = 13;
HexNumbAt[i] = (x*pow(16,k));
break;
case 'e':
x = 14;
HexNumbAt[i] = (x*pow(16,k));
break;
case 'f':
x = 15;
HexNumbAt[i] = (x*pow(16,k));
break;
}
}
k += 1;
DecNumb += HexNumbAt[i];
}
// Display the Decimal converted number;
printf("DecNumb = %d\n", DecNumb);
return 0;
}
DecToHex.c:
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int DecNumb = 0;
char HexNumb[20] = {0};
double Quotient = 1;
int Reminder = 0;
printf("Please Enter A Decilam: ");
scanf("%d", &DecNumb);
int i = 0;
Quotient = DecNumb;
while(Quotient != 0)
{
Reminder = ((int) Quotient)%16;
Quotient = Quotient/16;
Quotient = (int) Quotient;
if(Reminder >= 0 && Reminder <= 9)
{
sprintf(&HexNumb[i], "%d", Reminder);
}
else
if(Reminder >= 10 && Reminder <= 15)
{
switch(Reminder)
{
case 10: HexNumb[i] = 'A'; break;
case 11: HexNumb[i] = 'B'; break;
case 12: HexNumb[i] = 'C'; break;
case 13: HexNumb[i] = 'D'; break;
case 14: HexNumb[i] = 'E'; break;
case 15: HexNumb[i] = 'F'; break;
default: printf("Error: Unrecognized Hex Digit\n");
printf("Reminder = %d", Reminder);
return -1;
}
}
i++;
}
HexNumb[i] = '\0';
printf("HexNumb = ");
while(i >= 0)
{
printf("%c", HexNumb[i]);
i--;
}
printf("\n");
return 0;
}
Thank You for your feedback if there is any