Finished exercise 3.5
This commit is contained in:
@@ -10,10 +10,10 @@ 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
|
||||
gcc -c main.c -I"$hex_octal_conv_lib_include_dir" -std=gnu99 -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
|
||||
gcc bin/main.o -L"$hex_octal_conv_lib_dir" -lhexoctalconv -std=gnu99 -o bin/a.out -O3
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
*
|
||||
* Exercise 3-5. Write a function itob(n,s,b that converts the integer
|
||||
* Exercise 3-5. Write a function itob(n,s,b) that converts the integer
|
||||
* m into a base b character representation in the string s. In
|
||||
* particular, itob(n,s,16) formats s as a hexadecimal integer in s.
|
||||
*
|
||||
@@ -9,19 +9,79 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "hex.h"
|
||||
|
||||
// Only bases of 8 and 16 are supported. So Hexidecimal or octal
|
||||
// Returns -1 if invalid. and 1 if valid
|
||||
int itob(int n, char *s, int b);
|
||||
|
||||
void test();
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
test();
|
||||
// test();
|
||||
const int number = 128;
|
||||
const int base_value = 8;
|
||||
char value[1024];
|
||||
|
||||
int result = itob(number, value, base_value);
|
||||
|
||||
if (result == -1)
|
||||
{
|
||||
printf("Base %d is not supported\n", base_value);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (base_value == 8)
|
||||
{
|
||||
printf("%d is in octal (%d base) %s\n", number, base_value, value);
|
||||
}
|
||||
else if (base_value == 16)
|
||||
{
|
||||
printf("%d is in hexidecimal (%d base) %s\n", number, base_value, value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int itob(int n, char *s, int b)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (! (b == 8 || b == 16))
|
||||
{
|
||||
// Neither 8 or 16 based. Invalid
|
||||
result = -1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const int ARRAY_SIZE = 1024;
|
||||
|
||||
char number_value[ARRAY_SIZE];
|
||||
HexOctalConv_itoa(n, number_value, 10);
|
||||
|
||||
|
||||
// printf("Number %d is %s in string\n", n, number_value);
|
||||
|
||||
if (b == 8)
|
||||
{
|
||||
HexOctalConv_number_converted_to_octal(s, number_value);
|
||||
}
|
||||
else if (b == 16)
|
||||
{
|
||||
HexOctalConv_number_converted_to_hex(s, number_value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void test()
|
||||
{
|
||||
char *number = "21";
|
||||
|
||||
Reference in New Issue
Block a user