Laurin
11 months ago
3 changed files with 376 additions and 0 deletions
-
129src/conversionOfNumbers/conversionOfNumbers.c
-
31src/conversionOfNumbers/conversionOfNumbers.h
-
216test/conversionOfNumbers/test_conversionOfNumbers.c
@ -0,0 +1,129 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <math.h> |
|||
#include <string.h> |
|||
|
|||
#include "conversionOfNumbers.h" |
|||
|
|||
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; |
|||
if(rest == 1) result[length - 1] = '1'; |
|||
if(rest == 0) result[length - 1] = '0'; |
|||
length--; |
|||
}while(input != 0); |
|||
|
|||
return result; |
|||
} |
|||
|
|||
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; |
|||
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--; |
|||
}while(input != 0); |
|||
|
|||
return result; |
|||
} |
|||
|
|||
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; |
|||
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; |
|||
} |
@ -0,0 +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 |
@ -0,0 +1,216 @@ |
|||
#include "unity.h" |
|||
#include "conversionOfNumbers.h" |
|||
#include <stdlib.h> |
|||
|
|||
void setUp(){} |
|||
void tearDown(){} |
|||
|
|||
void test_convertNum2ToBinaryStr10(){ |
|||
//arrange |
|||
char* result; |
|||
char expected[] = "10"; |
|||
|
|||
//act |
|||
result = convertIntToBinaryStr(2); |
|||
|
|||
//assert |
|||
TEST_ASSERT_EQUAL_STRING(expected, result); |
|||
free(result); |
|||
} |
|||
|
|||
void test_binaryStringLengthOfInput5is3(){ |
|||
//arrange |
|||
int result; |
|||
int expected = 3; |
|||
|
|||
//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); |
|||
} |
|||
|
|||
void test_convertNum10ToBinaryStr1010(){ |
|||
//arrange |
|||
char* result; |
|||
char expected[] = "1010"; |
|||
|
|||
//act |
|||
result = convertIntToBinaryStr(10); |
|||
|
|||
//assert |
|||
TEST_ASSERT_EQUAL_STRING(expected, result); |
|||
free(result); |
|||
} |
|||
|
|||
void test_binaryStringLengthOfInputNegative10is5(){ |
|||
//arrange |
|||
int result; |
|||
int expected = 5; |
|||
|
|||
//act |
|||
result = binaryStrLen(-10); |
|||
|
|||
//assert |
|||
TEST_ASSERT_EQUAL_INT(expected, result); |
|||
} |
|||
|
|||
void test_convertNegativeNum12ToBinaryStrNegative1100(){ |
|||
//arrange |
|||
char* result; |
|||
char expected[] = "-1100"; |
|||
|
|||
//act |
|||
result = convertIntToBinaryStr(-12); |
|||
|
|||
//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); |
|||
} |
|||
|
|||
void test_convertBinaryStr1111ToInt15(){ |
|||
//arrange |
|||
unsigned int result; |
|||
unsigned int expected = 15; |
|||
|
|||
//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); |
|||
} |
|||
|
|||
void test_convertNum3ToHexStr3(){ |
|||
//arrange |
|||
char* result; |
|||
char expected[] = "3"; |
|||
|
|||
//act |
|||
result = convertIntToHex(3); |
|||
|
|||
//assert |
|||
TEST_ASSERT_EQUAL_STRING(expected, result); |
|||
free(result); |
|||
} |
|||
|
|||
void test_convertNum12ToHexStrC(){ |
|||
//arrange |
|||
char* result; |
|||
char expected[] = "C"; |
|||
|
|||
//act |
|||
result = convertIntToHex(12); |
|||
|
|||
//assert |
|||
TEST_ASSERT_EQUAL_STRING(expected, result); |
|||
free(result); |
|||
} |
|||
|
|||
void test_hexStringLengthOfInput256is3(){ |
|||
//arrange |
|||
int result; |
|||
int expected = 3; |
|||
|
|||
//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); |
|||
} |
|||
|
|||
void test_hexStringLengthOfInput0is1(){ |
|||
//arrange |
|||
int result; |
|||
int expected = 1; |
|||
|
|||
//act |
|||
result = hexStrLen(0); |
|||
|
|||
//assert |
|||
TEST_ASSERT_EQUAL_INT(expected, result); |
|||
} |
|||
|
|||
void test_convertNum234ToHexStrEA(){ |
|||
//arrange |
|||
char* result; |
|||
char expected[] = "EA"; |
|||
|
|||
//act |
|||
result = convertIntToHex(234); |
|||
|
|||
//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); |
|||
} |
|||
|
|||
void test_convertHexStr2A3ToInt675(){ |
|||
//arrange |
|||
unsigned int result; |
|||
unsigned int expected = 675; |
|||
|
|||
//act |
|||
result = convertHexStrToInt("2A3"); |
|||
|
|||
//assert |
|||
TEST_ASSERT_EQUAL_UINT(expected, result); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue