Added simple implementation of hex/octal conversion

This commit is contained in:
kdeng00
2020-10-29 21:47:51 -04:00
parent 72906bac35
commit 9639c6453e
8 changed files with 239 additions and 71 deletions
+19
View File
@@ -0,0 +1,19 @@
#!/bin/bash
if [ ! -d "bin" ]; then
mkdir bin
echo "Created bin directory"
fi
current_dir=`pwd`
hex_octal_conv_lib_root_dir="../../libraries/hex_octal_conv"
hex_octal_conv_lib_include_dir="$hex_octal_conv_lib_root_dir/include"
hex_octal_conv_lib_dir="$hex_octal_conv_lib_root_dir/bin/static"
gcc -c main.c -I"$hex_octal_conv_lib_include_dir" -o bin/main.o -O3
cd $hex_octal_conv_lib_root_dir
./compile.sh "release"
cd $current_dir
gcc bin/main.o -L"$hex_octal_conv_lib_dir" -lhexoctalconv -o bin/a.out -O3
+31
View File
@@ -10,8 +10,39 @@
#include <stdio.h>
#include "hex.h"
void test();
int main()
{
test();
return 0;
}
void test()
{
char *number = "21";
char hex_number[1024];
HexOctalConv_number_converted_to_hex(hex_number, number);
printf("%s converted to hex is %s\n", number, hex_number);
number = "55";
HexOctalConv_number_converted_to_hex(hex_number, number);
printf("%s converted to hex is %s\n", number, hex_number);
const int numerical_value = HexOctalConv_hex_converted_to_number(hex_number);
printf("Hexidecimal %s is %d\n", hex_number, numerical_value);
char octal_number[1024];
HexOctalConv_number_converted_to_octal(octal_number, number);
printf("%s converted to octal is %s\n", number, octal_number);
const int o_numberical_value = HexOctalConv_octal_converted_to_number(octal_number);
printf("Octal %s converted to a numerical value is %d\n", octal_number, o_numberical_value);
}
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
gcc -c main.c -I../../libraries/test_library1/include -o bin/main.o
cd ../../libraries/test_library1
./compile.sh
cd ../../experiments/call_library
gcc bin/main.o -L../../libraries/test_library1/bin/static -ltestlibrary -o bin/call_library
+19
View File
@@ -0,0 +1,19 @@
#include <stdio.h>
#include "test.h"
int main()
{
printf("Going to print test value from TestLibrary\n");
const double test_value = TestLibrary_test_value();
printf("%.0f\n", test_value);
const double new_test_value = TestLibrary_multiply_by_one_point_five(test_value);
printf("Multiplied by 1.5: %.2f\n", new_test_value);
return 0;
}
+19 -1
View File
@@ -1,3 +1,12 @@
#!/bin/bash
if [ -n "$1" ]; then
compile_type=$1
else
compile_type="debug"
fi
if [ ! -d "bin" ]; then
mkdir bin
echo "created bin directory"
@@ -8,6 +17,15 @@ if [ ! -d "bin/static" ]; then
echo "created bin/static directory"
fi
gcc -c src/hex.c -Iinclude -o bin/static/hex.o
if [ "$compile_type" = "debug" ]; then
echo "Compiling for debug version"
gcc -c src/hex.c -Iinclude -o bin/static/hex.o -O0
elif [ "$compile_type" = "release" ]; then
echo "Compiling for release version"
gcc -c src/hex.c -Iinclude -o bin/static/hex.o -O3
else
echo "Compiling for debug by default"
gcc -c src/hex.c -Iinclude -o bin/static/hex.o -O0
fi
ar rcs bin/static/libhexoctalconv.a bin/static/hex.o
+11 -9
View File
@@ -3,16 +3,18 @@
#define BUFFER_LIMIT 1024
char hex_value[BUFFER_LIMIT];
char *HexOctalConv_number_converted_to_hex(char *value);
int HexOctalConv_hex_converted_to_number(char *value);
int HexOctalConv_octal_converted_to_number(char *value);
int HexOctalConv_hex_part_to_number(char hex_part);
long HexOctalConv_rudy_pow(long value, int power);
int HexOctalConv_size_of_string(char *value);
char HexOctalConv_convert_hex_part(int value);
void HexOctalConv_reverse_order(char *value);
void HexOctalConv_number_converted_to_hex(char *hex_value, char *value);
void HexOctalConv_number_converted_to_octal(char *octal_value, char *value);
static long rudy_pow(long value, int power);
static int size_of_string(char *value);
static void reverse_order(char *value);
+129 -61
View File
@@ -4,45 +4,37 @@
#include <ctype.h>
char *HexOctalConv_number_converted_to_hex(char *value)
int HexOctalConv_hex_converted_to_number(char *value)
{
int number_value = atoi(value);
int string_size = HexOctalConv_size_of_string(value);
int number = 0;
int power = 0;
int string_size = size_of_string(value);
extern char hex_value[];
int hex_start = 0;
for (double number = number_value;;)
for (int i = (string_size - 1); i >= 0; --i)
{
const double quotient = number / 16.0;
const int quotient_trunecated = number / 16;
const double remaining = quotient - quotient_trunecated;
int number_hex_part = remaining * 16;
char hex_part = value[i];
int number_part = HexOctalConv_hex_part_to_number(hex_part);
number += number_part * rudy_pow(16, power);
char hex_part = HexOctalConv_convert_hex_part(number_hex_part);
hex_value[hex_start++] = hex_part;
/**
if (number >= 16)
{
}
else
{
}
*/
number = quotient_trunecated;
++power;
}
hex_value[hex_start] = '\0';
return number;
}
if (hex_start > 1)
int HexOctalConv_octal_converted_to_number(char *value)
{
int octal_converted_to_number = 0;
int power_value = 0;
for (int a = size_of_string(value) - 1; a >= 0; --a)
{
HexOctalConv_reverse_order(hex_value);
int octal_part = value[a] - '0';
int octal_power = octal_part * rudy_pow(8, power_value++);
octal_converted_to_number += octal_power;
}
return hex_value;
return octal_converted_to_number;
}
int HexOctalConv_hex_part_to_number(char hex_part)
@@ -78,36 +70,6 @@ int HexOctalConv_hex_part_to_number(char hex_part)
return result;
}
long HexOctalConv_rudy_pow(long value, int power)
{
if (power > 2)
{
return value * HexOctalConv_rudy_pow(value, --power);
}
else if (power == 2)
{
return value * value;
}
else if (power == 1)
{
return value;
}
else
{
return 1;
}
}
int HexOctalConv_size_of_string(char *value)
{
int size = 0;
for (; value[size] != '\0'; ++size);
return size;
}
char HexOctalConv_convert_hex_part(int value)
{
@@ -142,9 +104,115 @@ char HexOctalConv_convert_hex_part(int value)
}
void HexOctalConv_reverse_order(char *value)
void HexOctalConv_number_converted_to_hex(char *hex_value, char *value)
{
int string_size = HexOctalConv_size_of_string(value);
printf("Starting process to convert %s to hexidecimal\n", value);
int number_value = atoi(value);
int string_size = size_of_string(value);
int hex_start = 0;
for (double number = number_value;;)
{
const double quotient = number / 16.0;
const int quotient_trunecated = number / 16;
const double remaining = quotient - quotient_trunecated;
int number_hex_part = remaining * 16;
char hex_part = HexOctalConv_convert_hex_part(number_hex_part);
hex_value[hex_start++] = hex_part;
// printf("%s\n", hex_value);
if (number < 16)
{
break;
}
number = quotient_trunecated;
}
hex_value[hex_start] = '\0';
if (hex_start > 1)
{
reverse_order(hex_value);
}
}
void HexOctalConv_number_converted_to_octal(char *octal_value, char *value)
{
int value_number = atoi(value);
int index = 0;
for (double quotient_number = value_number; (quotient_number >= 0);)
{
if (quotient_number == 0)
{
octal_value[index] = '0';
break;
}
else
{
quotient_number /= 8;
int trunecated_quotient_number = quotient_number;
double remaining = quotient_number - trunecated_quotient_number;
int octal_part = remaining * 8;
octal_value[index] = octal_part + '0';
quotient_number = trunecated_quotient_number;
++index;
}
}
octal_value[index] = '\0';
for (int a = 0; a <= (index / 2); ++a)
{
char tmp = octal_value[a];
octal_value[a] = octal_value[(index - 1) - a];
octal_value[(index - 1)] = tmp;
}
}
static long rudy_pow(long value, int power)
{
if (power > 2)
{
return value * rudy_pow(value, --power);
}
else if (power == 2)
{
return value * value;
}
else if (power == 1)
{
return value;
}
else
{
return 1;
}
}
static int size_of_string(char *value)
{
int size = 0;
for (; value[size] != '\0'; ++size);
return size;
}
static void reverse_order(char *value)
{
int string_size = size_of_string(value);
for (int i = 0;; ++i)
{