# 14+ C Hexadecimal to Decimal Converters

This post contains a total of 14+ C Hexadecimal to Decimal Converter examples. All the Hexadecimal to decimal converters are made using C Programming language.

You can use the source codes of these programs for your own personal or educational use with credits to the original owner.

### Related Posts

Click a Code to Copy it.

## 1. By Arun Tomar

Made by Arun Tomar. Input must be hexadecimal format like input 0xa–>hexadecimal output 10–>decimal. ( Source )

``````#include <stdio.h>

int main() {
int i;
printf("enter any number");
scanf("%i",&i);
printf ("%d",i);
return 0;

}``````

## 2. By Er. Suraj Sahu

Made by Er. Suraj Sahu. Simple C Hexadecimal to Decimal Converter program. ( Source )

``````#include <stdio.h>

int main() {
int n;
scanf("%x",&n);
printf("%d",n);

return 0;
}``````

## 3. By Ashy

Made by Ashy. Provide hexadecimal number as a string input starting with 0x and followed by the hex numbers. ( Source )

``````//Conversion of hexadecimal to decimal
//input format: "0x____"

#include<stdio.h>
int convert (char *);
int main()
{
char a[10];
scanf("%s",a);

int r =convert(a+2);
printf ("input   : %s\n",a);
printf ("output :%d\n",r);
}
int convert (char *p)
{
int i,d=0,s=0;
for(i=0;p[i];i++)
{
if((p[i]>='0')&&(p[i]<='9'))
d=p[i]-'0';
else if((p[i]>='a')&&(p[i]<='f'))
d=p[i]-97+10;
else if((p[i]>='A')&&(p[i]<='F'))
d=p[i]-65+10;
;
s=s*16+d;
}
return s;
}``````

## 4. By Malek Alsset

Made by Malek Alsset. ( Source )

``````#include<stdio.h>
int main(void)
{
int n;
scanf("%x",&n);
printf("%d",n);
} ``````

## 5. By Aravind Allemgari

Made by Aravind Allemgari. ( Source )

``````#include <stdio.h>

int main()
{
int a;
printf("enter the decimal value:\n");
scanf("%d",&a);
printf("equivalent hexadecimal value of %d is %x",a,a);
return 0;
}``````

## 6. By Noor ❤

Made by Noor ❤. ( Source )

``````
/*Convert hexdecimal to decimal

Input : 12a
Output: 298

*/

#include <stdio.h>
#include<string.h>
#include<math.h>

int main() {
int i ,place;
long int sum;
char str[12];
scanf("%s",str);
place = strlen(str);
place--;

for(i=0; str[i]!='\0'; i++,place--)
{
if(str[i]>='0' && str[i]<='9')
{
str[i] = str[i]-'0';
}

else if(str[i]>='A' && str[i]<='F')
{
str[i] = str[i]-65+10;
}

else if(str[i]>='a' && str[i]<='f')
{
str[i] = str[i]-97+10;
}
else
{
printf("\nInvalid");
return ;

}

sum = sum + (str[i]*pow(16,place));
}

printf("%ld",sum);

return 0;
}``````

## 7. By Easa

``````// Hexadecimal to decimal

#include<stdio.h>
void main()
{
int hex_n=0,remainder,decimal_n=0,i=0;
scanf("%d",&hex_n);
while(hex_n != 0)
{
remainder = hex_n % 10;
decimal_n += remainder * pow(16,i);
hex_n/=10;
i++;
}
printf("\n The Decimal equivalent = %d",decimal_n);
}``````

## 8. By Kguarian

Made by Kguarian. ( Source )

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TRUE 1
int main()
{
char *uInput;
char *processedInput;
int numStartIndex;
char currChar = 0;
int msdIndex = 0;
int lsdIndex = -1;
int inputLengthLimit = 20;
int i, j;

uInput = (char *)malloc(inputLengthLimit * sizeof(char));                   //create buffer
scanf("%s", uInput);                                                        //scan user input into buffer
processedInput = (char *)calloc(inputLengthLimit * sizeof(char), 1);        //limit user input to predefined buffer size
for (i = 0; i < inputLengthLimit; i++)
*(processedInput + i) = *(uInput + i);

if (*processedInput == '0')                                                 //set msdIndex to 2 if first 2 characters form "0x" or "0X"
{
if (*(processedInput + 1) == 'x' || *(processedInput + 1) == 'X')
{
msdIndex = 2;
}
}

//find least significant digit: parse from high to low index, find first nonzero value (0-init'd array)
//if its index is not at least that of the first digit, error message and exit.
for (i = inputLengthLimit; i >= msdIndex; i--)
{
if (*(processedInput + i) != 0)
{
lsdIndex = i;
break;
}
}
for (i = msdIndex; i <= lsdIndex; i++)              //validating character input. Set of accepted characters = all char values in ['0'-'9'] U ['A','F'] U ['a','f'].
{
if (!((*(processedInput + i) >= 'a' && *(processedInput + i) <= 'f') || (*(processedInput + i) >= 'A' && *(processedInput + i) <= 'F') || (*(processedInput + i) >= '0' && *(processedInput + i) <= '9')))
{
lsdIndex = -1;
break;
}
}
if (lsdIndex == -1)
{
printf("Invalid Input.");
return EXIT_SUCCESS;
}

//remember: hex = sum(16^(digitPosition) * digitValue)
int sum = 0;
for (i = msdIndex; i <= lsdIndex; i++)
{
switch (*(processedInput + i))
{
int currDigit;

case '0':                                   // if character is 0, do nothing. It does not add to the sum.
break;
case '1':                                   //if character is 1
currDigit = 1;                          //set base digit to 1
for (j = 0; j < lsdIndex - i; j++)      //multiply it by 16^(LEAST_SIGNIFICANT_DIGIT_INDEX - CURRENT_INDEX) and...
{
currDigit *= 16;
}
sum += currDigit;                       //add the resultant product to the sum.
break;
case '2':
currDigit = 2;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case '3':
currDigit = 3;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case '4':
currDigit = 4;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case '5':
currDigit = 5;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case '6':
currDigit = 6;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case '7':
currDigit = 7;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case '8':
currDigit = 8;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case '9':
currDigit = 9;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'a':
currDigit = 10;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'b':
currDigit = 11;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'c':
currDigit = 12;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'd':
currDigit = 13;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'e':
currDigit = 14;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'f':
currDigit = 15;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'A':
currDigit = 10;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'B':
currDigit = 11;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'C':
currDigit = 12;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'D':
currDigit = 13;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'E':
currDigit = 14;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
case 'F':
currDigit = 15;
for (j = 0; j < lsdIndex - i; j++)
{
currDigit *= 16;
}
sum += currDigit;
break;
default:
fprintf(stderr, "debug: default hit\n");
break;
}
}

printf("%d\n", sum);
}``````

## 9. By Mantra-Jain

Made by Mantra-Jain. ( Source )

``````#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
char hex[20];
long long decimal = 0, base = 1;
int i = 0, value, length;
scanf("%s",hex);
length = strlen(hex);
for(i = length--; i >= 0; i--)
{
if(hex[i] >= '0' && hex[i] <= '9')
{
decimal += (hex[i] - 48) * base;
base *= 16;
}
else if(hex[i] >= 'A' && hex[i] <= 'F')
{
decimal += (hex[i] - 55) * base;
base *= 16;
}
else if(hex[i] >= 'a' && hex[i] <= 'f')
{
decimal += (hex[i] - 87) * base;
base *= 16;
}
}
printf("Decimal number = %lld\n", decimal);
return 0;
}``````

## 10. By Pa9526

Made by Pa9526. ( Source )

``````#include<stdio.h>
#include<math.h>
long long convert(int);
int main(){
int number,bin;char in;float decimal;
printf("Enter the Number to be coverted ");
scanf("%d",&number);
printf(" It will be converted to:\n1. octal 2. Hexadecimal 3. decimal 4. binary\n");
printf("%o in Octal \n",number);
decimal=number;
printf("%.2f in decimal \n",decimal);
bin = convert(number);
}
long long convert(int number) {
long long bin = 0;
int rem, i = 1;

while (number!=0) {
rem = number % 2;
number /= 2;
bin += rem * i;
i *= 10;}
printf("%d in Binary \n",bin);
}``````

## 11. By Dct2012

Made by Dct2012. Converts hexadecimal to decimal or vice versa. ( Source )

``````#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
//#include <unistd.h> //TODO: eventually add getopt
#include <math.h>

void help();
char* itoa(int, int);
void print_everything(int, char*, char*);
void predict_type(char*);
void decimal(char*);
void binary(char*);
void easy_mode(char*);
void loop(void (*f)(char*), char*);

int main(int argc, char *argv[])
{
if(argc == 1 || strcmp(argv[1], "--help") == 0)
help();

else if (argc == 2)
predict_type(argv[1]);

else if(strcmp(argv[1], "-d") == 0)
decimal(argv[2]);

else if(strcmp(argv[1], "-b") == 0)
binary(argv[2]);

else if(strcmp(argv[1], "-h") == 0)

else if(strcmp(argv[1], "-e") == 0)
easy_mode(argv[2]);

else if(strcmp(argv[1], "-l") == 0)
loop(decimal, argv[2]);

else
puts("error! try --help\n");

return 0;
}

void help()
{
puts("--help         display help page");
puts("-h (hex)       to convert hex to binary and decimal");
puts("-b (binary)    to convert binary to hex and decimal");
puts("-d (decimal)   to convert decimal to binary and hex");
puts("-e (decimal)   convert using printf, etc");
puts("-l (loop)      continue to ask for (decimals) to convert");
}

void print_everything(int d, char *b, char *h)
{
printf("Decimal:       %d\n", d);
printf("Binary:        %s\n", b);
}

void error_message(char *e)
{
printf("Error! insert a valid %s.\n", e);
}

char* itoa(int val, int base)
{
int size = 0;
int temp = val;
int n_bytes = 0;
for(; temp; size++, temp /= base)
if(size % 8 == 0)
n_bytes++;

if(n_bytes == 0)
n_bytes++;

char *buf = malloc(n_bytes);

buf[size] = '\0';
for(int i = --size; val; i--, val /= base)
buf[i] = "0123456789ABCDEF"[val % base];

return buf;
}

{
//TODO: try to consolidate these 2 conditions
for (int i = 0; i < 16; i++)
if("0123456789ABCDEF"[i] == hex || "0123456789abcdef"[i] == hex)
return i;
}

int char_array_length(char *a)
{
int size;
for(size = 0; a[size + 1]; size++);

return size;
}

bool is_decimal(char *d)
{
// the chars 0-9 = int values 48-57
for(int i = 0; d[i]; i++)
if(d[i] < 48 || d[i] > 57)
return false;

return true;
}

int char_to_int(char* d, int size)
{
int val = 0;

for(int i = 0; d[i]; i++)
{
val += (d[i] - '0') * pow(10, size);

size--;
}

return val;
}

void decimal(char *decimal)
{
if(is_decimal(decimal))
{
int number = char_to_int(decimal, char_array_length(decimal));
char *b;
char *h;
print_everything(number, b = itoa(number, 2), h = itoa(number, 16));
free(b);
free(h);
}
else
error_message("decimal");
}

{
// the chars 0-9 = int values 48-57
// the chars A-F = int values 65-70
// the chars a-f = int values 97-102
for(int i = 0; h[i]; i++)
if(h[i] < 48 || h[i] > 102 || (h[i] > 57 && h[i] < 65) || (h[i] > 70 && h[i] < 97))
return false;

return true;
}

{
int val = 0;

for(int i = 0; h[i]; i++)
{
val += hexadecimal_char_to_int(h[i]) * pow(16, size);

size--;
}

return val;
}

//TODO: if hexadecimal is lowercase print it uppercase
//      print hexadecimals at lengths of 2, 4, ...
{
{
char *b;
print_everything(i, b = itoa(i, 2), hex);
free(b);
}
else
}

bool is_binary(char* b)
{
for(int i = 0; b[i]; i++)
if(b[i] != '0' && b[i] != '1')
return false;

return true;
}

int binary_to_int(char *b, int size)
{
int val = 0;

for(int i = 0; b[i]; i++)
{
if(b[i] == '1')
val += pow(2, size);

size--;
}

return val;
}

//TODO: print binary at lengths of 8, 16, ...
void binary(char *bin)
{
if(is_binary(bin))
{
char *h;
int i = binary_to_int(bin, char_array_length(bin));
print_everything(i, bin, h = itoa(i, 16));
free(h);
}
else
error_message("binary");
}

void easy_mode(char *c)
{
if(is_decimal(c))
{
int d = char_to_int(c, char_array_length(c));
char *b;

printf("Decimal:       %d\n", d);
printf("Binary:        %s\n", b = itoa(d, 2)); //I dont think there's a shorter way for binary

free(b);
}
else
error_message("decimal");
}

void predict_type(char *t)
{
if(is_binary(t))
binary(t);
else if (is_decimal(t))
decimal(t);
else
}

void loop(void (*f)(char*), char* c)
{
bool run = true;

while(run)
{
(*f)(c);
printf("\nNext decimal (or q to quit): ");
if(scanf("%s", c) > 0)
{
if(c[0] == 'q' || c[0] == 'Q')
run = false;
}
}
}``````

## 12. By larasous

Made by larasous. A numeric base converter, hexadecimal to decimal and vice versa. ( Source )

``````#include <stdio.h>
#include <stdlib.h>

printf("------------------------------\n");
printf("------------------------------\n");
}

int main(int argc, char *argv[]){

int op, val;
printf("------------------------------\n");
printf(" Conversor de Bases Numericas \n");
scanf("%d", &op);
getchar();

if(op == 1){

printf("------------------------------\n");
printf("Informe o valor em decimal: ");
scanf("%d", &val);
getchar();

printf("------------------------------\n");
printf("%d em hexadecimal e: %x\n", val, val);

} else if(op == 2) {

printf("------------------------------\n");
printf("Informe o valor em hexadecimal: ");
scanf("%x", &val);
getchar();

printf("------------------------------\n");
printf("%x em hexadecimal e: %d\n", val, val);

} else {

printf("------------------------------\n");
printf("Opção Inválida!\nTente Novamente");
}

system('PAUSE');
return 0;
}``````

## 13. By AvariCe-git

Made by AvariCe-git. ( Source )

``````/*  This "program" converts hexadecimal, octal, or binary numbers to decimals.
It also converts decimals to either hexadecimal, octal or binary mumbers.
To use it, here are the arguments passed from the command line:
d 'prefix''number': converts a number to decimal from binary, octal or hexadecimal.
Each number has to have the appropriate prefix: 0x for hex, 0o for octal
and 0b for binary, followed immediately by the number without a space.
eg: whatever -d 0xabc, to convert hex abc to decimal.
b decimal_number: converts a decimal number to binary.
eg whatever b 159
o decimal_number: converts a decimal to octal.
h decimal_number: converts a decimal to hexadecimal.
Current version is 1.4
2020 Antonios Giannakopoulos, https://github.com/AvariCe-git */

#include <stdio.h>                                                                  // Standard input output
#include <math.h>                                                                   // Needed for raising to powers
#include <stdlib.h>                                                                 // Needed for atoi
#include <string.h>                                                                 // Needed for strcmp
#include <stdbool.h>                                                                // Needed for boolean variables
#include <string.h>                                                                 // Needed for strlen

void check(char base[],char number[]);                                              // Checks the arguments and calls the appropriate functions
void convert_to(int base, int number);                                              // Displays the convertion of a decimal to hexadecimal, octal or binary
void print_hex(int number);                                                         // Helps output the conversion result
void convert_from(char arg1[]);                                                     // Converts a hexadecimal, octal or binary number to decimal
void print_help();                                                                  // Prints the help

int main(int argc,char* argv[]){

check(argv[1],argv[2]);                                                         // Starts the whole shebang
return 0;

}

void check(char base[], char number[]){                                             // Checks the arguments and calls the appropriate functions

int i = 0;

i = atoi(number);
if(strcmp(base, "b") == 0)
convert_to(2,i);
else if(strcmp(base, "o") == 0)
convert_to(8,i);
else if (strcmp(base, "h") == 0)
convert_to(16,i);
else if (strcmp(base, "d") == 0)
convert_from(number);
else
print_help();

}

void convert_to(int base, int number){                                              // Displays the convertion of a decimal to hexadecimal, octal or binary
// It accepts two arguments: the base for the conversion (integer) and the decimal
bool trip = 0;                                                                  // number to convert to(integer)
char specifics[3][15] = {"binary","octal","hexadecimal"};                       // Helps with displaying the conversion message
char prefix[3][5] = {"0b","0o","0x"};                                           // Same
int i = 0;                                                                      // It's the counter
int bin[40];                                                                    // The array where the converted number lives
for(i = 0; i < 40; i++)                                                         // Initialise the array
bin[i] = 0;
printf("%d in %s is:  %s ", number, specifics[base/8],prefix[base/8]);          // Prints the message according to the base the user wants
for(i=0; i<40; i++){                                                            // Here the conversion happens. Every repetion, the array gets the remainder of the
bin[i] = number%base;                                                       // given number divided by the given base, and then it gets divided by the given base until
number = number/base;                                                       // it reaches the end of the array. I could have it break out of the loop the moment
}                                                                               // number/base is zero, but I'm lazy and it suits my needs as is.
for (i=39; i>=0; i--){                                                          // Here it prints the conversion. It goes from the end of the array to the start.
if(trip == 0){                                                              // First it checks if the boolean is false. If it is, it searches for a non-zero number.
if(bin[i] != 0){                                                        //  When it's found, the boolean changes state, calls the print_hex,which prints the aforementioned number
trip = 1;                                                           //  and stores the array's location, for the purpose of showing the biggest power of given number.
print_hex(bin[i]);                                                  //  If the boolean is tripped, it means a non zero number has been found and after that it prints every character
number = i;                                                         // in the array.  It's done so it only disregards the non important zeroes in the array.
}
}
else
print_hex(bin[i]);
}
printf("\n");
printf("Highest power of %d is:  %d", base, number);

}

void print_hex(int number){  // Helps output the conversion result.
// I used a separate function for that because the code above
char hex_extra[6][2] = {"A","B","C","D","E","F"};                               // was getting a little heavy for my tastes. It accepts a single integer as argument,
if(number > 9)                                                                  // and if it's bigger than nine it means that it belongs to a hex number and prints
printf("%s", hex_extra[number-10]);                                         // the appropriate letter. Otherwise it prints the given number.
else
printf("%d", number);

}

void convert_from(char arg1[]){                                                     // Converts a hexadecimal, octal or binary number to decimal
// It accepts a character array. It scans the first two characters to determine
int i = 0, result = 0,temp = 0,length = 0;                                      // which base it converts from, and converts accordingly. The formula for every
length = strlen(arg1);                                                          // base is this for every loop:
if(strncmp(arg1,"0b",2) == 0){                                                  // conversion_number = conversion_number + array[i]*(given_base ^ (array[i]-i-1)
for(i = 2; i < length; i++){                                                // It basically raises the base to the appropriate number in every iteration,
temp = arg1[i] - '0';                                                   // multiplies it to the number it finds in the array and adds it to the result.
result = result + temp*pow(2,length-i-1);                               // It reads the number from the array by removing the ascii offset
}                                                                           // the (-'0' part) and casting it to integer.
printf("%d", result);                                                       // For hexadecimal specifically, if the number it reads after removing the offset
}                                                                               // is in the range of 17 -> 22 or 49 -> 54 means it's either A->F or a->f respectively.
else if(strncmp(arg1,"0o",2) == 0){                                             // In that case, it just sets the read number to its proper value
for(i = 2; i < length; i++){
temp = arg1[i];
result = result + temp*pow(8,length-i-1);
}
printf("%d", result);
}
else if(strncmp(arg1,"0x",2) == 0){
for(i = 2; i < length; i++){
temp = arg1[i] - '0';
if(temp == 17 || temp == 49)
temp = 10;
if(temp == 18 || temp == 50)
temp = 11;
if(temp == 19 || temp == 51)
temp = 12;
if(temp == 20 || temp == 52)
temp = 13;
if(temp == 21 || temp == 53)
temp = 14;
if(temp == 22 || temp == 54)
temp = 15;
result = result + temp*pow(16,length-i-1);
}
printf("%d", result);
}

}

void  print_help(){                                                                 // Shows the help

printf("This ""program"" converts hexadecimal, octal, or binary numbers to decimals.\n");
printf(" It also converts decimals to either hexadecimal, octal or binary mumbers.\n");
printf("To use it, here are the arguments passed from the command line:\n");
printf("d 'prefix''number': converts a number to decimal from binary, octal or hexadecimal.\n");
printf("    Each number has to have the appropriate prefix: 0x for hex, 0o for octal\n");
printf("    and 0b for binary, followed immediately by the number without a space.\n");
printf("    eg: whatever -d 0xabc, to convert hex abc to decimal.\n");
printf("b decimal_number: converts a decimal number to binary.\n");
printf("    eg whatever b 159\n");
printf("o decimal_number: converts a decimal to octal.\n");
printf("h decimal_number: converts a decimal to hexadecimal.\n");
printf("Current version is 1.4\n");
printf("2020 Antonios Giannakopoulos, https://github.com/AvariCe-git\n");

}``````

``````#include <math.h>

void convert_hex_to_dec(int number) {
int i=0,remaining,sum=0;

while(number>0) {
remaining = number %10;
number/=10;
sum += remaining * pow(16,i);
i++;

}
printf("Decimal:%d\n",sum);
}``````

## 15. By oskaerik

Made by oskaerik. A simple tool to convert a number between the decimal, hexadecimal number systems. The program uses 32-bit unsigned integers and is designed to work properly with numbers in the interval [0, 4294967295], the behavior of the program is undefined when using numbers outside the interval or with improperly formatted numbers. ( Source )

``````/* Oskar Eriksson */
#include <stdio.h>

/* Returns 1 if array contains to_check, 0 if not */
int contains(int *array, int size, int to_check) {
for (int i = 0; i < size; i++) {
if (*(array + i) == to_check) {
// Found match, return 1
return 1;
}
}
// No match, return 0
return 0;
}

/* Adds valid input to an array */
void get_input(int *array, int size) {
int valid_size = 24;
int valid[] = {
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
// A, B, C, D, E, F, X
65, 66, 67, 68, 69, 70, 88,
// a, b, c, d, e, f, x
97, 98, 99, 100, 101, 102, 120
};
// Get input from standard in
int current_char = 0;
for (int i = 0; i < size; i++) {
current_char = getchar();
if (current_char == EOF) {
break;
} else if (contains(valid, valid_size, current_char)) {
// Add char if valid, invalid remains as 0
*(array + i) = current_char;
}
}
}

/* Returns decimal value of binary number from raw input */
int bin_input(int *array, int size) {
int result = 0;
for (int i = 0; i < size; i++) {
if (*(array + i) == 48) {
// Was 0, shift left
result = result << 1;
} else if (*(array + i) == 49) {
// Was 1, shift left and add 1
result = result << 1;
result++;
}
}
return result;
}

/* Returns decimal value of hexadecimal number from raw input */
int hex_input(int *array, int size) {
int result = 0;
int factor = 1;
// Go through backwards, skip first two indexes
for (int i = size - 1; i > 1; i--) {
if (*(array + i) >= 48 && *(array + i) <= 57) {
// Was 0-9, add value times factor, then increase factor
result += (*(array + i) - 48) * factor;
factor *= 16;
} else if (*(array + i) >= 65 && *(array + i) <= 70) {
// Was A-F, add value times factor, then increase factor
result += (*(array + i) - 55) * factor;
factor *= 16;
} else if (*(array + i) >= 97 && *(array + i) <= 102) {
// Was a-f, add value times factor, then increase factor
result += (*(array + i) - 87) * factor;
factor *= 16;
}
}
return result;
}

/* Returns decimal value of decimal number from raw input */
int dec_input(int *array, int size) {
int result = 0;
int factor = 1;
// Go through backwards, skip first two indexes
for (int i = size - 1; i >= 0; i--) {
if (*(array + i) >= 48 && *(array + i) <= 57) {
// Was 0-9, add value times factor, then increase factor
result += (*(array + i) - 48) * factor;
factor *= 10;
}
}
return result;
}

/* Prints a number in the decimal number system */
void print_dec(unsigned int number) {
printf("Dec: %u\n", number);
}

/* Prints a number in the binary number system */
void print_bin(unsigned int number) {
printf("Bin: ");
unsigned int factor = 2147483648;
int started = 0;
while (1) {
if (number >= factor) {
number -= factor;
printf("1");
started = 1;
} else {
if (started) {
printf("0");
}
}
factor /= 2;
if (factor < 1) {
// Break when done printing
break;
}
}
if (!started) {
// Print a zero if nothing has been printed
printf("0");
}
printf("\n");
}

/* Prints a number in the hexadecimal number system */
void print_hex(unsigned int number) {
printf("Hex: 0x");
unsigned int factor = 268435456;
int started = 0;
int counter = 0;
while (1) {
counter = 0;
while (number >= factor) {
number -= factor;
counter += 1;
started = 1;
}
if (started) {
if (counter > 9) {
printf("%c", counter + 55);
} else {
printf("%d", counter);
}
}
factor /= 16;
if (factor < 1) {
// Break when done printing
break;
}
}
if (!started) {
// Print a zero if nothing has been printed
printf("0");
}
printf("\n");
}

/* Main function */
int main() {
// Get valid input
int size = 64;
int input[64] = {0};
get_input(input, size);

// Check base and get decimal value
unsigned int decimal = 0;
if (input[0] == 'b' || input[0] == 'B') {
// Binary starts with b/B
decimal = bin_input(input, size);
} else if (input[0] == '0' && (input[1] == 'x' || input[1] == 'X')) {