Hex converter half finished. Can convert numbers to hex

This commit is contained in:
kdeng00
2020-10-08 23:04:16 -04:00
parent a37d67cb54
commit ae52c8d321
5 changed files with 392 additions and 62 deletions
+2
View File
@@ -0,0 +1,2 @@
compile with math library using gcc:
gcc main.c -lm
+46 -42
View File
@@ -1,6 +1,6 @@
/*
*
* Write a program that can convert a decimal to a hex value and vice-versa.
* Write a program that can convert a number to a hex value and vice-versa.
* The program must pass a command line argument to determine what will
* be the conversion type and the value to be converted. The value
* must be evaluated to make sure it's valid before converting.
@@ -14,16 +14,16 @@
#include <math.h>
#define OCTAL 1
#define DECIMAL 2
#define NUMBER 2
// Returns 1 if octal conversion was chosen
// Returns 2 if decimal conversion was chosen
// Returns 2 if number conversion was chosen
// Returns 0 or non-zero number if not valid
int valid_conversion_option(char *option);
int valid_conversion_target(char *target);
// Returns 1 if valid and 0 if the decimal value isn't
int valid_decimal_value(char *value);
// Returns 1 if valid and 0 if the number value isn't
int valid_number_value(char *value);
// Returns 1 if valid and 0 if the octal value isn't
int valid_octal_value(char *value);
@@ -33,41 +33,45 @@ int valid_octal_value(char *value);
int size_of_value(char *value);
// Converts decimal value to octal and prints the value
// Converts number value to octal and prints the value
void convert_to_octal(char *value);
// Converts octal value to decimal and prints the value
void convert_to_decimal(char *value);
// Converts octal value to number and prints the value
void convert_to_number(char *value);
int main(int argc, char **argv)
{
if (argc < 3)
{
printf("Provide two arguments: ./program [conversion_option] [decimal | octal value]\n");
printf("Provide two arguments:\n");
printf("./program [conversion_target] [number | octal value]\n");
printf("[conversion_target] - oct or num as the target\n");
printf("oct - Converts the number to a hex value\n");
printf("num - Converts the octal value to a number\n");
return -1;
}
char *conversion_option = argv[1];
int result = valid_conversion_option(conversion_option);
char *conversion_target = argv[1];
int result = valid_conversion_target(conversion_target);
if (result < 1)
{
printf("Invalid conversion option %s.\noct and dec are only supported\n", conversion_option);
printf("Invalid conversion target %s.\noct and num are only supported\n", conversion_target);
return -1;
}
printf("%s is a valid conversion option\n", conversion_option);
printf("%s is a valid conversion target\n", conversion_target);
switch (result)
{
case OCTAL:
if (valid_decimal_value(argv[2]) == 0)
if (valid_number_value(argv[2]) == 0)
{
printf ("Invalid decimal value %s\n", argv[2]);
printf ("Invalid number value %s\n", argv[2]);
return -1;
}
@@ -75,7 +79,7 @@ int main(int argc, char **argv)
convert_to_octal(argv[2]);
break;
case DECIMAL:
case NUMBER:
if (valid_octal_value(argv[2]) == 0)
{
printf("Invalid octal value %s\n", argv[2]);
@@ -83,7 +87,7 @@ int main(int argc, char **argv)
return -1;
}
convert_to_decimal(argv[2]);
convert_to_number(argv[2]);
break;
}
@@ -93,12 +97,12 @@ int main(int argc, char **argv)
}
int valid_conversion_option(char *option)
int valid_conversion_target(char *target)
{
const int OPTION_SIZE = 4;
// Supported conversion options
// Supported conversion targets
char *oct = "oct";
char *dec = "dec";
char *num = "num";
int result = 0;
@@ -106,7 +110,7 @@ int valid_conversion_option(char *option)
for (int a = 0; a != OPTION_SIZE; ++a)
{
// Checking to see if octal conversion is selected
if (option[a] != oct[a])
if (target[a] != oct[a])
{
break;
}
@@ -119,8 +123,8 @@ int valid_conversion_option(char *option)
for (int a = 0; a != OPTION_SIZE; ++a)
{
// Checking to see if decimal conversion is selected
if (option[a] != dec[a])
// Checking to see if number conversion is selected
if (target[a] != num[a])
{
break;
}
@@ -135,7 +139,7 @@ int valid_conversion_option(char *option)
return result;
}
int valid_decimal_value(char *value)
int valid_number_value(char *value)
{
int result = 1;
@@ -143,11 +147,11 @@ int valid_decimal_value(char *value)
{
// ASCII value 48 is 0. ASCII value 57 is 9.
// If value contains any non-numbers then
// the value isn't a valid decimal value
// the value isn't a valid number value
if (value[a] < 48 || value[a] > 57)
{
result = 0;
printf("Invalid character for decimal %c\n", value[a]);
printf("Invalid character for number %c\n", value[a]);
break;
}
@@ -190,31 +194,31 @@ int size_of_value(char *value)
void convert_to_octal(char *value)
{
int value_decimal = atoi(value);
int value_number = atoi(value);
char octal_value[12] = "00000000000\0";
int index = 0;
printf("Converting %d to octal\n", value_decimal);
printf("Converting %d to octal\n", value_number);
for (double quotient_decimal = value_decimal; (quotient_decimal >= 0);)
for (double quotient_number = value_number; (quotient_number >= 0);)
{
if (quotient_decimal == 0)
if (quotient_number == 0)
{
octal_value[index] = '0';
break;
}
else
{
quotient_decimal /= 8;
int trunecated_quotient_decimal = quotient_decimal;
double remaining = quotient_decimal - trunecated_quotient_decimal;
quotient_number /= 8;
int trunecated_quotient_number = quotient_number;
double remaining = quotient_number - trunecated_quotient_number;
int octal_part = remaining * 8;
printf("Octal part %d index %d remaining %f\n", octal_part, index, quotient_decimal);
printf("Octal part %d index %d remaining %f\n", octal_part, index, quotient_number);
octal_value[index] = octal_part + '0';
quotient_decimal = trunecated_quotient_decimal;
quotient_number = trunecated_quotient_number;
++index;
}
@@ -231,24 +235,24 @@ void convert_to_octal(char *value)
}
printf("Decimal value %d is %s in octal\n", value_decimal, octal_value);
printf("Decimal value %d is %s in octal\n", value_number, octal_value);
}
void convert_to_decimal(char *value)
void convert_to_number(char *value)
{
int octal_converted_to_decimal = 0;
int octal_converted_to_number = 0;
int power_value = 0;
printf("Converting %s to decimal\n", value);
printf("Converting %s to number\n", value);
for (int a = size_of_value(value) - 1; a >= 0; --a)
{
int octal_part = value[a] - '0';
int octal_power = octal_part * pow(8, power_value++);
octal_converted_to_decimal += octal_power;
octal_converted_to_number += octal_power;
printf("Octal part %d octal power %d\n", octal_part, octal_power);
}
printf("Octal value %s is %d in decimal\n", value, octal_converted_to_decimal);
printf("Octal value %s is %d in number\n", value, octal_converted_to_number);
}
-10
View File
@@ -1,10 +0,0 @@
/*
*
* Write a program that can convert a decimal to a hex value and vice-versa.
* The program must pass a command line argument to determine what will
* be the conversion type and the value to be converted. The value
* must be evaluated to make sure it's valid before converting.
* Otherwise, inform the user and terminate the program
*
* Author: Kun Deng
*/