From d335bfcb5f534692cb9bc17a9d60e84ec46e454e Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 22:46:55 +0100 Subject: [PATCH 01/46] defined conversionOfNumbers.h --- src/conversionOfNumbers/conversionOfNumbers.h | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/conversionOfNumbers/conversionOfNumbers.h diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h new file mode 100644 index 0000000..2b5e90c --- /dev/null +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -0,0 +1,6 @@ +#ifndef CONVERSIONOFNUMBERS_H +#define CONVERSIONOFNUMBERS_H + +char* convertIntToBinaryStr(int input); + +#endif \ No newline at end of file From 7b115251919813a49fdef0ab9bdd30dc2999bb71 Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 23:13:09 +0100 Subject: [PATCH 02/46] added function to convert int from 0 - 3 into binary string --- src/conversionOfNumbers/conversionOfNumbers.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/conversionOfNumbers/conversionOfNumbers.c diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c new file mode 100644 index 0000000..64dae01 --- /dev/null +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -0,0 +1,19 @@ +#include +#include + +char* convertIntToBinaryStr(int input){ + if(input > 3 || input < 0) return NULL; + int length = 2; + int rest; + char* result = (char*)malloc(sizeof(char) * length + 1); + int index = length - 1; + do{ + rest = input % 2; + input /= 2; + if(rest == 1) result[index] = '1'; + if(rest == 0) result[index] = '0'; + index--; + }while(input != 0); + result[length] = '\0'; + return result; +} \ No newline at end of file From 7c0bee3c9f055b1adf3fe0cb3d88b6b37eefd7c3 Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 23:15:32 +0100 Subject: [PATCH 03/46] added test to convert 2 into binary string --- .../test_conversionOfNumbers.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 test/conversionOfNumbers/test_conversionOfNumbers.c diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c new file mode 100644 index 0000000..2490896 --- /dev/null +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -0,0 +1,17 @@ +#include "unity.h" +#include "conversionOfNumbers.h" + +void setUp(){} +void tearDown(){} + +void test_convert2ToBinaryStr(){ + //arrange + char* result; + char expected[] = "10"; + + //act + result = convertIntToBinaryStr(2); + + //assert + TEST_ASSERT_EQUAL_STRING(expected, result); +} \ No newline at end of file From 501ebf2245962ff7b219cda5d1c271c0bdd6153e Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 23:26:25 +0100 Subject: [PATCH 04/46] refactoring: used length for indexing instead of index --- src/conversionOfNumbers/conversionOfNumbers.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 64dae01..b7ae51b 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -6,14 +6,13 @@ char* convertIntToBinaryStr(int input){ int length = 2; int rest; char* result = (char*)malloc(sizeof(char) * length + 1); - int index = length - 1; + result[length] = '\0'; do{ rest = input % 2; input /= 2; - if(rest == 1) result[index] = '1'; - if(rest == 0) result[index] = '0'; - index--; + if(rest == 1) result[length - 1] = '1'; + if(rest == 0) result[length - 1] = '0'; + length--; }while(input != 0); - result[length] = '\0'; return result; } \ No newline at end of file From c91005ceb8f0fee394fd207e30d21b29a14924e7 Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 23:34:17 +0100 Subject: [PATCH 05/46] declared new function to determine length of binary string in header file --- src/conversionOfNumbers/conversionOfNumbers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h index 2b5e90c..92e1d85 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.h +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -2,5 +2,6 @@ #define CONVERSIONOFNUMBERS_H char* convertIntToBinaryStr(int input); +int binaryStrLen(int input); #endif \ No newline at end of file From 08f6485826e376ef4e7d078c825020149f685ec8 Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 23:54:26 +0100 Subject: [PATCH 06/46] added function to determine binary string length --- src/conversionOfNumbers/conversionOfNumbers.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index b7ae51b..d614913 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -1,5 +1,19 @@ #include #include +#include + +#include "conversionOfNumbers.h" + +int binaryStrLen(int input){ + int length; + for(int x = 0; x < input; x++){ + if(pow(2,x) >= input + 1){ + length = x; + break; + } + } + return length; +} char* convertIntToBinaryStr(int input){ if(input > 3 || input < 0) return NULL; From 9430c473b1a21f2de981f090a593f4e6801d9652 Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 23:56:39 +0100 Subject: [PATCH 07/46] added test for determining binary string length --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 2490896..c049622 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -14,4 +14,16 @@ void test_convert2ToBinaryStr(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); +} + +void test_binaryStringLengthOfInput5is3(){ + //arrange + int result; + int expected = 3; + + //act + result = binaryStrLen(5); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); } \ No newline at end of file From e504c53d9f38fd1c3063625f7ede00e9e1c0ed91 Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 00:02:51 +0100 Subject: [PATCH 08/46] added case in binaryStrLen for input 0 length is 1 --- src/conversionOfNumbers/conversionOfNumbers.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index d614913..9c77555 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -6,13 +6,17 @@ int binaryStrLen(int input){ int length; + if(input > 0){ for(int x = 0; x < input; x++){ if(pow(2,x) >= input + 1){ length = x; break; } } - return length; + }else if(input == 0){ + length = 1; + } + return length; } char* convertIntToBinaryStr(int input){ From bb18fb7e4b37fe1be93f4a393062770e15244f21 Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 00:05:23 +0100 Subject: [PATCH 09/46] added test for binaryStrLen in case input is 0 --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index c049622..114b83a 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -24,6 +24,18 @@ void test_binaryStringLengthOfInput5is3(){ //act result = binaryStrLen(5); + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_binaryStringLengthOfInput0is1(){ + //arrange + int result; + int expected = 1; + + //act + result = binaryStrLen(0); + //assert TEST_ASSERT_EQUAL_INT(expected, result); } \ No newline at end of file From aa2e397bec7d3d0db8d01f9cb50f0578dc56955e Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 00:52:45 +0100 Subject: [PATCH 10/46] fix added smaller equal sign in loop head --- src/conversionOfNumbers/conversionOfNumbers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 9c77555..c71b53e 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -7,7 +7,7 @@ int binaryStrLen(int input){ int length; if(input > 0){ - for(int x = 0; x < input; x++){ + for(int x = 0; x <= input; x++){ if(pow(2,x) >= input + 1){ length = x; break; From 6fd5e28d8cf9da1cf2fe54582624319af2e6a9c9 Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 00:58:35 +0100 Subject: [PATCH 11/46] implemented binaryStrLen function into convertIntToBinaryStr --- src/conversionOfNumbers/conversionOfNumbers.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index c71b53e..7bf51b2 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -20,8 +20,7 @@ int binaryStrLen(int input){ } char* convertIntToBinaryStr(int input){ - if(input > 3 || input < 0) return NULL; - int length = 2; + int length = binaryStrLen(input); int rest; char* result = (char*)malloc(sizeof(char) * length + 1); result[length] = '\0'; From 3b81a29e10223532fc91feb1da872421cd937119 Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 01:02:03 +0100 Subject: [PATCH 12/46] added test for implementation of binaryStrLen function --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 114b83a..2a4e777 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -38,4 +38,16 @@ void test_binaryStringLengthOfInput0is1(){ //assert TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_convert10ToBinaryStr(){ + //arrange + char* result; + char expected[] = "1010"; + + //act + result = convertIntToBinaryStr(10); + + //assert + TEST_ASSERT_EQUAL_STRING(expected, result); } \ No newline at end of file From bc51648c31d211dff45fbfa9391c91269f1f3c41 Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 01:09:00 +0100 Subject: [PATCH 13/46] refactoring: changed test function names to be more specific --- test/conversionOfNumbers/test_conversionOfNumbers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 2a4e777..a823000 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -4,7 +4,7 @@ void setUp(){} void tearDown(){} -void test_convert2ToBinaryStr(){ +void test_convert2ToBinaryStr10(){ //arrange char* result; char expected[] = "10"; @@ -40,7 +40,7 @@ void test_binaryStringLengthOfInput0is1(){ TEST_ASSERT_EQUAL_INT(expected, result); } -void test_convert10ToBinaryStr(){ +void test_convert10ToBinaryStr1010(){ //arrange char* result; char expected[] = "1010"; From 69d57301a1f3217c1e042b1c38a59282cfb0c205 Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 11:22:50 +0100 Subject: [PATCH 14/46] refactoring: moved if statement for input 0 to top --- src/conversionOfNumbers/conversionOfNumbers.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 7bf51b2..fdaf2e8 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -6,16 +6,13 @@ int binaryStrLen(int input){ int length; - if(input > 0){ - for(int x = 0; x <= input; x++){ - if(pow(2,x) >= input + 1){ - length = x; - break; - } + if (input == 0) return 1; + for (int x = 0; x <= input; x++){ + if(pow(2,x) >= input + 1){ + length = x; + break; + } } - }else if(input == 0){ - length = 1; - } return length; } From fb4ad3c51b11b51a558ee3319254ba0590d0512e Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 11:37:58 +0100 Subject: [PATCH 15/46] expanded binaryStrLen to determine length in case input is negative --- src/conversionOfNumbers/conversionOfNumbers.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index fdaf2e8..86af510 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -5,11 +5,15 @@ #include "conversionOfNumbers.h" int binaryStrLen(int input){ - int length; + int length = 0; if (input == 0) return 1; + if (input < 0){ + input *= -1; + length += 1; + } for (int x = 0; x <= input; x++){ if(pow(2,x) >= input + 1){ - length = x; + length += x; break; } } From 01a54ce8363963a906794d4df741d0a1b1e72fe6 Mon Sep 17 00:00:00 2001 From: Laurin Date: Wed, 31 Jan 2024 11:40:24 +0100 Subject: [PATCH 16/46] added test for binaryStrLen in case input is negative --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index a823000..fe9441a 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -50,4 +50,16 @@ void test_convert10ToBinaryStr1010(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); +} + +void test_binaryStringLengthOfInputNegative10is5(){ + //arrange + int result; + int expected = 5; + + //act + result = binaryStrLen(-10); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); } \ No newline at end of file From abf4d186ad1b5a3466a1382b61686d3e22f5fbe6 Mon Sep 17 00:00:00 2001 From: Laurin Date: Thu, 1 Feb 2024 12:45:51 +0100 Subject: [PATCH 17/46] added if statement to be able to convert negative integers --- src/conversionOfNumbers/conversionOfNumbers.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 86af510..1160eab 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -24,6 +24,10 @@ char* convertIntToBinaryStr(int input){ int length = binaryStrLen(input); int rest; char* result = (char*)malloc(sizeof(char) * length + 1); + if(input < 0){ + result[0] = '-'; + input *= -1; + } result[length] = '\0'; do{ rest = input % 2; From f68d81069719765e7509aef0df20f307cb9e81bb Mon Sep 17 00:00:00 2001 From: Laurin Date: Thu, 1 Feb 2024 12:47:08 +0100 Subject: [PATCH 18/46] added test for converting negative integer --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index fe9441a..4bf3272 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -62,4 +62,16 @@ void test_binaryStringLengthOfInputNegative10is5(){ //assert TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_convertNegative12ToBinaryStrNegative1100(){ + //arrange + char* result; + char expected[] = "-1100"; + + //act + result = convertIntToBinaryStr(-12); + + //assert + TEST_ASSERT_EQUAL_STRING(expected, result); } \ No newline at end of file From f36bc32ed71a7be1ee24e7f1bcc09ed9829aa1e5 Mon Sep 17 00:00:00 2001 From: Laurin Date: Thu, 1 Feb 2024 12:52:42 +0100 Subject: [PATCH 19/46] refactoring: free allocated memory in test functions --- test/conversionOfNumbers/test_conversionOfNumbers.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 4bf3272..a5132dc 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -1,5 +1,6 @@ #include "unity.h" #include "conversionOfNumbers.h" +#include void setUp(){} void tearDown(){} @@ -14,6 +15,7 @@ void test_convert2ToBinaryStr10(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); + free(result); } void test_binaryStringLengthOfInput5is3(){ @@ -50,6 +52,7 @@ void test_convert10ToBinaryStr1010(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); + free(result); } void test_binaryStringLengthOfInputNegative10is5(){ @@ -74,4 +77,5 @@ void test_convertNegative12ToBinaryStrNegative1100(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); + free(result); } \ No newline at end of file From 3648f419cd71e4b9c0879000c5b131903ed80eda Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 09:12:03 +0100 Subject: [PATCH 20/46] declared new function in header file to convert binary numbers into decimal --- src/conversionOfNumbers/conversionOfNumbers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h index 92e1d85..9fb79c1 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.h +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -3,5 +3,6 @@ char* convertIntToBinaryStr(int input); int binaryStrLen(int input); +unsigned int convertBinaryStrToInt(char* input); #endif \ No newline at end of file From a7218c6a732cdbd73a6397ddbf8260a0d9a7bf94 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 09:21:59 +0100 Subject: [PATCH 21/46] implemented prototype function to convert binary strings of 1s into decimal --- src/conversionOfNumbers/conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 1160eab..4c97fbf 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -37,4 +37,16 @@ char* convertIntToBinaryStr(int input){ length--; }while(input != 0); return result; +} + +unsigned int convertBinaryStrToInt(char* input){ + unsigned int result = 0; + int x = 0; + while(input[x] != '\0'){ + if(input[x] == '1'){ + result += 1 * pow(2, x); + } + x++; + } + return result; } \ No newline at end of file From e48b4f38a61a028fdf7c63b76e230816446e3337 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 09:23:44 +0100 Subject: [PATCH 22/46] added test for converting binary string of 1s to decimal --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index a5132dc..954bdd5 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -78,4 +78,16 @@ void test_convertNegative12ToBinaryStrNegative1100(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); free(result); +} + +void test_convertBinaryStr11ToInt3(){ + //arrange + unsigned int result; + unsigned int expected = 3; + + //act + result = convertBinaryStrToInt("11"); + + //assert + TEST_ASSERT_EQUAL_UINT(expected, result); } \ No newline at end of file From c7c45916fe7213f619c687f5ed1a68c4330724f0 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 09:26:57 +0100 Subject: [PATCH 23/46] added test for converting longer binary string of 1s to decimal --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 954bdd5..5659461 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -88,6 +88,18 @@ void test_convertBinaryStr11ToInt3(){ //act result = convertBinaryStrToInt("11"); + //assert + TEST_ASSERT_EQUAL_UINT(expected, result); +} + +void test_convertBinaryStr1111ToInt15(){ + //arrange + unsigned int result; + unsigned int expected = 15; + + //act + result = convertBinaryStrToInt("1111"); + //assert TEST_ASSERT_EQUAL_UINT(expected, result); } \ No newline at end of file From bd55ea228be03dc75e15be20158cde3e08181ecd Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 14:10:07 +0100 Subject: [PATCH 24/46] added test for converting binary string of 1s and 0s to decimal --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 5659461..c3f6ffe 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -100,6 +100,18 @@ void test_convertBinaryStr1111ToInt15(){ //act result = convertBinaryStrToInt("1111"); + //assert + TEST_ASSERT_EQUAL_UINT(expected, result); +} + +void test_convertBinaryStr1001ToInt9(){ + //arrange + unsigned int result; + unsigned int expected = 9; + + //act + result = convertBinaryStrToInt("1001"); + //assert TEST_ASSERT_EQUAL_UINT(expected, result); } \ No newline at end of file From 7dfe49145c3a1310bb1fb05c4b30f0352db05401 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:07:46 +0100 Subject: [PATCH 25/46] declared new function to convert int to hex in header file --- src/conversionOfNumbers/conversionOfNumbers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h index 9fb79c1..3422e06 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.h +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -4,5 +4,6 @@ char* convertIntToBinaryStr(int input); int binaryStrLen(int input); unsigned int convertBinaryStrToInt(char* input); +char* convertIntToHex(int input); #endif \ No newline at end of file From 9cbb10f12502aa12bea504523ebe6dc92f5feb14 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:10:57 +0100 Subject: [PATCH 26/46] added function to convert int to hex string for small ints --- src/conversionOfNumbers/conversionOfNumbers.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 4c97fbf..6c9708c 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -49,4 +49,20 @@ unsigned int convertBinaryStrToInt(char* input){ x++; } return result; +} + +char* convertIntToHex(int input){ + int length = 1; + int rest; + char* result = (char*)malloc(sizeof(char) * length + 1); + result[length] = '\0'; + do{ + rest = input % 16; + input /= 16; + switch(rest){ + default: result[length - 1] = rest + '0'; break; + } + length--; + }while(input != 0); + return result; } \ No newline at end of file From 9fbe4606e1ee72998326daf4232c2d541d8368da Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:12:23 +0100 Subject: [PATCH 27/46] added test for converting small int to hex string --- test/conversionOfNumbers/test_conversionOfNumbers.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index c3f6ffe..4ab94ea 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -114,4 +114,17 @@ void test_convertBinaryStr1001ToInt9(){ //assert TEST_ASSERT_EQUAL_UINT(expected, result); +} + +void test_convert3ToHexStr3(){ + //arrange + char* result; + char expected[] = "3"; + + //act + result = convertIntToHex(3); + + //assert + TEST_ASSERT_EQUAL_STRING(expected, result); + free(result); } \ No newline at end of file From a96a8759116f0cfbd1be52840c41662518578427 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:23:08 +0100 Subject: [PATCH 28/46] added other cases for input being smaller than 16 --- src/conversionOfNumbers/conversionOfNumbers.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 6c9708c..6a92ed7 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -60,6 +60,12 @@ char* convertIntToHex(int input){ rest = input % 16; input /= 16; switch(rest){ + case 10: result[length - 1] = 'A'; break; + case 11: result[length - 1] = 'B'; break; + case 12: result[length - 1] = 'C'; break; + case 13: result[length - 1] = 'D'; break; + case 14: result[length - 1] = 'E'; break; + case 15: result[length - 1] = 'F'; break; default: result[length - 1] = rest + '0'; break; } length--; From 943f911a82a8526f25415b7d93023611f99bb308 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:29:08 +0100 Subject: [PATCH 29/46] added test for converting into hex for numbers smaller than 16 --- test/conversionOfNumbers/test_conversionOfNumbers.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 4ab94ea..c978215 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -124,6 +124,19 @@ void test_convert3ToHexStr3(){ //act result = convertIntToHex(3); + //assert + TEST_ASSERT_EQUAL_STRING(expected, result); + free(result); +} + +void test_convert12ToHexStrC(){ + //arrange + char* result; + char expected[] = "C"; + + //act + result = convertIntToHex(12); + //assert TEST_ASSERT_EQUAL_STRING(expected, result); free(result); From ed0ba8f0b8047d2fd2b754ae4aa9934d2b1a70cf Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:47:45 +0100 Subject: [PATCH 30/46] declared function to determine string length of hex string --- src/conversionOfNumbers/conversionOfNumbers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h index 3422e06..b897a66 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.h +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -5,5 +5,6 @@ char* convertIntToBinaryStr(int input); int binaryStrLen(int input); unsigned int convertBinaryStrToInt(char* input); char* convertIntToHex(int input); +int hexStrLen(int input); #endif \ No newline at end of file From f68766a844ef95ea4af652e2aacb64bff440b3f1 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:48:46 +0100 Subject: [PATCH 31/46] implemented function for determining string length of hex string --- src/conversionOfNumbers/conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 6a92ed7..0d828bc 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -51,6 +51,18 @@ unsigned int convertBinaryStrToInt(char* input){ return result; } +int hexStrLen(int input){ + int length = 0; + if(input == 0) return 1; + for (int x = 0; x <= input; x++){ + if(pow(16,x) >= input + 1){ + length += x; + break; + } + } + return length; +} + char* convertIntToHex(int input){ int length = 1; int rest; From e66f206d793f3ec1428b923a5a2c4d8c3662efbe Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:49:26 +0100 Subject: [PATCH 32/46] added test for hexStrLen function --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index c978215..a878c81 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -140,4 +140,16 @@ void test_convert12ToHexStrC(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); free(result); +} + +void test_binaryStringLengthOfInput256is3(){ + //arrange + int result; + int expected = 3; + + //act + result = hexStrLen(256); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); } \ No newline at end of file From 69da91f715ff396bb414c2fa19f7b701a8972aed Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 15:53:26 +0100 Subject: [PATCH 33/46] refactoring: corrected tes function name --- test/conversionOfNumbers/test_conversionOfNumbers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index a878c81..e5fd37a 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -142,7 +142,7 @@ void test_convert12ToHexStrC(){ free(result); } -void test_binaryStringLengthOfInput256is3(){ +void test_hexStringLengthOfInput256is3(){ //arrange int result; int expected = 3; From 2faaa84310e8be278d8277914c2b9f60e81f586a Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 16:01:47 +0100 Subject: [PATCH 34/46] added test to make sure hexStrLen function behaves as corrected --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index e5fd37a..897bcb9 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -150,6 +150,18 @@ void test_hexStringLengthOfInput256is3(){ //act result = hexStrLen(256); + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_hexStringLengthOfInput255is2(){ + //arrange + int result; + int expected = 2; + + //act + result = hexStrLen(255); + //assert TEST_ASSERT_EQUAL_INT(expected, result); } \ No newline at end of file From 4032750fa9de13fda559ea8825fd66d15d45ceb1 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 16:05:00 +0100 Subject: [PATCH 35/46] added test to test special case of input 0 --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 897bcb9..5b11535 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -162,6 +162,18 @@ void test_hexStringLengthOfInput255is2(){ //act result = hexStrLen(255); + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_hexStringLengthOfInput0is1(){ + //arrange + int result; + int expected = 1; + + //act + result = hexStrLen(0); + //assert TEST_ASSERT_EQUAL_INT(expected, result); } \ No newline at end of file From 2800426e7ccf93fc44043bea3fd4b9411a7b0def Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 16:08:41 +0100 Subject: [PATCH 36/46] implemented hexStrLen function into to convert function to dynamically determine hex string length --- src/conversionOfNumbers/conversionOfNumbers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 0d828bc..456801d 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -64,7 +64,7 @@ int hexStrLen(int input){ } char* convertIntToHex(int input){ - int length = 1; + int length = hexStrLen(input); int rest; char* result = (char*)malloc(sizeof(char) * length + 1); result[length] = '\0'; From bfaccfaecd9678359d6f4ff29e42dadb6625ea90 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 16:11:38 +0100 Subject: [PATCH 37/46] added test for implementation of hexStrLen function into convertIntToHex function --- test/conversionOfNumbers/test_conversionOfNumbers.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index 5b11535..c29c097 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -176,4 +176,17 @@ void test_hexStringLengthOfInput0is1(){ //assert TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_convert234ToHexStrEA(){ + //arrange + char* result; + char expected[] = "EA"; + + //act + result = convertIntToHex(234); + + //assert + TEST_ASSERT_EQUAL_STRING(expected, result); + free(result); } \ No newline at end of file From 16405fd4755b7294db57da2757b61a313fd1c030 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 20:24:25 +0100 Subject: [PATCH 38/46] refactoring: changed read only parameter of function from int to const int --- src/conversionOfNumbers/conversionOfNumbers.c | 2 +- src/conversionOfNumbers/conversionOfNumbers.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 456801d..305d0ad 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -51,7 +51,7 @@ unsigned int convertBinaryStrToInt(char* input){ return result; } -int hexStrLen(int input){ +int hexStrLen(const int input){ int length = 0; if(input == 0) return 1; for (int x = 0; x <= input; x++){ diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h index b897a66..d8ec7ce 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.h +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -5,6 +5,6 @@ char* convertIntToBinaryStr(int input); int binaryStrLen(int input); unsigned int convertBinaryStrToInt(char* input); char* convertIntToHex(int input); -int hexStrLen(int input); +int hexStrLen(const int input); #endif \ No newline at end of file From bc133aa9f1e778745bb994779cd8a4c9efe944b1 Mon Sep 17 00:00:00 2001 From: Laurin Date: Fri, 2 Feb 2024 20:31:28 +0100 Subject: [PATCH 39/46] refactoring: handled possible memory allocation errors in both convert functions --- src/conversionOfNumbers/conversionOfNumbers.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 305d0ad..fadc59a 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -24,6 +24,7 @@ char* convertIntToBinaryStr(int input){ int length = binaryStrLen(input); int rest; char* result = (char*)malloc(sizeof(char) * length + 1); + if(result == NULL) return NULL; if(input < 0){ result[0] = '-'; input *= -1; @@ -67,6 +68,7 @@ char* convertIntToHex(int input){ int length = hexStrLen(input); int rest; char* result = (char*)malloc(sizeof(char) * length + 1); + if(result == NULL) return NULL; result[length] = '\0'; do{ rest = input % 16; From 2fe8d2dcbffb0299212cfea71b6b7f647db8afb2 Mon Sep 17 00:00:00 2001 From: Laurin Date: Sat, 3 Feb 2024 10:40:56 +0100 Subject: [PATCH 40/46] fixed function converting binary input to decimal not working for every input --- src/conversionOfNumbers/conversionOfNumbers.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index fadc59a..337c1d8 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "conversionOfNumbers.h" @@ -42,12 +43,12 @@ char* convertIntToBinaryStr(int input){ unsigned int convertBinaryStrToInt(char* input){ unsigned int result = 0; - int x = 0; - while(input[x] != '\0'){ - if(input[x] == '1'){ - result += 1 * pow(2, x); - } - x++; + int index = strlen(input) - 1; + int exponent = 0; + while(index >= 0){ + if(input[index] == '1') result += 1 * pow(2, exponent); + index--; + exponent++; } return result; } From 3b11e3b13f51a5e8f3120198f9050c73eab35451 Mon Sep 17 00:00:00 2001 From: Laurin Date: Sat, 3 Feb 2024 10:43:08 +0100 Subject: [PATCH 41/46] added final test for converting binary input to decimal --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index c29c097..b9a0429 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -189,4 +189,16 @@ void test_convert234ToHexStrEA(){ //assert TEST_ASSERT_EQUAL_STRING(expected, result); free(result); +} + +void test_convertBinaryStr10110ToInt22(){ + //arrange + unsigned int result; + unsigned int expected = 22; + + //act + result = convertBinaryStrToInt("10110"); + + //assert + TEST_ASSERT_EQUAL_UINT(expected, result); } \ No newline at end of file From ded7c60fcbbd44cd9fdda1144e4086a58eb0a045 Mon Sep 17 00:00:00 2001 From: Laurin Date: Sat, 3 Feb 2024 10:54:55 +0100 Subject: [PATCH 42/46] defined and implemented function to convert hex input to decimal --- src/conversionOfNumbers/conversionOfNumbers.c | 20 +++++++++++++++++++ src/conversionOfNumbers/conversionOfNumbers.h | 1 + 2 files changed, 21 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 337c1d8..7b8667f 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -86,4 +86,24 @@ char* convertIntToHex(int input){ length--; }while(input != 0); return result; +} + +unsigned int convertHexStrToInt(char* input){ + unsigned int result = 0; + int index = strlen(input) - 1; + int exponent = 0; + while(index >= 0){ + switch(input[index]){ + case 'A': case 'a': result += 10 * pow(16, exponent); break; + case 'B': case 'b': result += 11 * pow(16, exponent); break; + case 'C': case 'c': result += 12 * pow(16, exponent); break; + case 'D': case 'd': result += 13 * pow(16, exponent); break; + case 'E': case 'e': result += 14 * pow(16, exponent); break; + case 'F': case 'f': result += 15 * pow(16, exponent); break; + default: result += (input[index] - '0') * pow(16, exponent); break; + } + index--; + exponent++; + } + return result; } \ No newline at end of file diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h index d8ec7ce..3718fcc 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.h +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -6,5 +6,6 @@ int binaryStrLen(int input); unsigned int convertBinaryStrToInt(char* input); char* convertIntToHex(int input); int hexStrLen(const int input); +unsigned int convertHexStrToInt(char* input); #endif \ No newline at end of file From db5f6c11069cbd8e118c9a5208785531e9f6b83c Mon Sep 17 00:00:00 2001 From: Laurin Date: Sat, 3 Feb 2024 10:55:50 +0100 Subject: [PATCH 43/46] added test for converting hex input to decimal --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index b9a0429..db390e7 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -199,6 +199,18 @@ void test_convertBinaryStr10110ToInt22(){ //act result = convertBinaryStrToInt("10110"); + //assert + TEST_ASSERT_EQUAL_UINT(expected, result); +} + +void test_convertHexStr2A3ToInt675(){ + //arrange + unsigned int result; + unsigned int expected = 675; + + //act + result = convertHexStrToInt("2A3"); + //assert TEST_ASSERT_EQUAL_UINT(expected, result); } \ No newline at end of file From 004b81aca4d896786bb445f3670360c57f3b2dd5 Mon Sep 17 00:00:00 2001 From: Laurin Date: Sat, 3 Feb 2024 11:42:31 +0100 Subject: [PATCH 44/46] refactoring: added comments describing the function's behavior and use in header file --- src/conversionOfNumbers/conversionOfNumbers.h | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.h b/src/conversionOfNumbers/conversionOfNumbers.h index 3718fcc..c42a225 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.h +++ b/src/conversionOfNumbers/conversionOfNumbers.h @@ -1,11 +1,31 @@ #ifndef CONVERSIONOFNUMBERS_H #define CONVERSIONOFNUMBERS_H +// This function converts an integer into a string of according binary digits. If the integer is below zero, +// a minus sign will be added in front of the string of binary digits. +// For the sake of simplicity there will be no use of the two's complement. +// Note: if allocation of memory doesn't work correctly, function will return NULL. char* convertIntToBinaryStr(int input); + +// This function determines the length of the string needed for the representation of an integer in binary digits. int binaryStrLen(int input); + +// This function converts a string of binary digits into the according decimal number. +// For the sake of simplicity it can only convert positive numbers into unsigned integers. unsigned int convertBinaryStrToInt(char* input); + +// This function converts an integer into an according string representing the integer in the hexadecimal system. +// Note: Need to check if the input integer is positive as implementation for converting negative integers +// is not done yet (for the sake of time). +// Note: if allocation of memory doesn't work correctly, function will return NULL. char* convertIntToHex(int input); + +// This function determines the length of the string needed for the representation of an integer +// in the hexadecimal system. int hexStrLen(const int input); + +// This function converts a hexadecimal number as a string into the according decimal number. +// For the sake of simplicity it can only convert positive numbers into unsigned integers. unsigned int convertHexStrToInt(char* input); #endif \ No newline at end of file From 26343283e427215cf87b7d4ba6a77c782c285697 Mon Sep 17 00:00:00 2001 From: Laurin Date: Mon, 5 Feb 2024 11:43:42 +0100 Subject: [PATCH 45/46] refactoring: made code more readable and added comments --- src/conversionOfNumbers/conversionOfNumbers.c | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 7b8667f..7fc35aa 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -7,30 +7,38 @@ int binaryStrLen(int input){ int length = 0; + if (input == 0) return 1; + // Length for sign bit, working with negative numbers: if (input < 0){ input *= -1; length += 1; } + // Calculate length needed for binary string: for (int x = 0; x <= input; x++){ if(pow(2,x) >= input + 1){ length += x; break; } } + return length; } char* convertIntToBinaryStr(int input){ int length = binaryStrLen(input); int rest; + char* result = (char*)malloc(sizeof(char) * length + 1); if(result == NULL) return NULL; + // Add sign bit: if(input < 0){ result[0] = '-'; input *= -1; } + // Terminate string: result[length] = '\0'; + // Algorithm to build binary string: do{ rest = input % 2; input /= 2; @@ -38,6 +46,7 @@ char* convertIntToBinaryStr(int input){ if(rest == 0) result[length - 1] = '0'; length--; }while(input != 0); + return result; } @@ -45,32 +54,40 @@ unsigned int convertBinaryStrToInt(char* input){ unsigned int result = 0; int index = strlen(input) - 1; int exponent = 0; + // Algorithm to calculate number for given binary string: while(index >= 0){ if(input[index] == '1') result += 1 * pow(2, exponent); index--; exponent++; } + return result; } int hexStrLen(const int input){ int length = 0; + if(input == 0) return 1; + // Calculate length needed for hexadecimal string: for (int x = 0; x <= input; x++){ if(pow(16,x) >= input + 1){ length += x; break; } } + return length; } char* convertIntToHex(int input){ int length = hexStrLen(input); int rest; + char* result = (char*)malloc(sizeof(char) * length + 1); if(result == NULL) return NULL; + // Terminate string: result[length] = '\0'; + // Algorithm to build hexadecimal string: do{ rest = input % 16; input /= 16; @@ -85,6 +102,7 @@ char* convertIntToHex(int input){ } length--; }while(input != 0); + return result; } @@ -92,6 +110,7 @@ unsigned int convertHexStrToInt(char* input){ unsigned int result = 0; int index = strlen(input) - 1; int exponent = 0; + // Algorithm to calculate number for given hexadecimal string: while(index >= 0){ switch(input[index]){ case 'A': case 'a': result += 10 * pow(16, exponent); break; @@ -105,5 +124,6 @@ unsigned int convertHexStrToInt(char* input){ index--; exponent++; } + return result; } \ No newline at end of file From 315547fddfedff3c206385b92d85b5762a4e8892 Mon Sep 17 00:00:00 2001 From: Laurin Date: Mon, 5 Feb 2024 11:55:07 +0100 Subject: [PATCH 46/46] refactoring: renamed some test functions --- test/conversionOfNumbers/test_conversionOfNumbers.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/conversionOfNumbers/test_conversionOfNumbers.c b/test/conversionOfNumbers/test_conversionOfNumbers.c index db390e7..b73d552 100644 --- a/test/conversionOfNumbers/test_conversionOfNumbers.c +++ b/test/conversionOfNumbers/test_conversionOfNumbers.c @@ -5,7 +5,7 @@ void setUp(){} void tearDown(){} -void test_convert2ToBinaryStr10(){ +void test_convertNum2ToBinaryStr10(){ //arrange char* result; char expected[] = "10"; @@ -42,7 +42,7 @@ void test_binaryStringLengthOfInput0is1(){ TEST_ASSERT_EQUAL_INT(expected, result); } -void test_convert10ToBinaryStr1010(){ +void test_convertNum10ToBinaryStr1010(){ //arrange char* result; char expected[] = "1010"; @@ -67,7 +67,7 @@ void test_binaryStringLengthOfInputNegative10is5(){ TEST_ASSERT_EQUAL_INT(expected, result); } -void test_convertNegative12ToBinaryStrNegative1100(){ +void test_convertNegativeNum12ToBinaryStrNegative1100(){ //arrange char* result; char expected[] = "-1100"; @@ -116,7 +116,7 @@ void test_convertBinaryStr1001ToInt9(){ TEST_ASSERT_EQUAL_UINT(expected, result); } -void test_convert3ToHexStr3(){ +void test_convertNum3ToHexStr3(){ //arrange char* result; char expected[] = "3"; @@ -129,7 +129,7 @@ void test_convert3ToHexStr3(){ free(result); } -void test_convert12ToHexStrC(){ +void test_convertNum12ToHexStrC(){ //arrange char* result; char expected[] = "C"; @@ -178,7 +178,7 @@ void test_hexStringLengthOfInput0is1(){ TEST_ASSERT_EQUAL_INT(expected, result); } -void test_convert234ToHexStrEA(){ +void test_convertNum234ToHexStrEA(){ //arrange char* result; char expected[] = "EA";