From 98f9ae391ff3d2c5655b3d69df775d42cf55f2f4 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Fri, 31 Jul 2020 13:35:15 +0000 Subject: [PATCH 1/2] replaced itoa with snprintf & fixed other docs --- conversions/int_to_string.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/conversions/int_to_string.c b/conversions/int_to_string.c index e31af2f96..3a56a04c0 100644 --- a/conversions/int_to_string.c +++ b/conversions/int_to_string.c @@ -1,9 +1,11 @@ /** * @file - * @brief Convert integer to string (non-standard function) + * @brief Convert a positive integer to string (non-standard function) + * representation. */ #include +#include #include #include #include @@ -13,14 +15,15 @@ * Converts an integer value to a null-terminated string using the specified * base and stores the result in the array given by str parameter. * @param value Value to be converted to a string. - * @param dest Array in memory where to store the resulting null-terminated + * @param dest pointer to array in memory to store the resulting null-terminated * string. * @param base Numerical base used to represent the value as a string, between 2 * and 16, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary. * @returns A pointer to the resulting null-terminated string, same as parameter * str. + * @note The destination array must be pre-allocated by the calling function. */ -char *int_to_string(int value, char dest[], int base) +char *int_to_string(uint16_t value, char *dest, int base) { const char hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; @@ -57,14 +60,14 @@ static void test() /* Generate value from 0 to 100 */ int value = rand() % 100; - assert(strcmp(itoa(value, str1, 2), int_to_string(value, str2, 2)) == - 0); - assert(strcmp(itoa(value, str1, 8), int_to_string(value, str2, 8)) == - 0); - assert(strcmp(itoa(value, str1, 10), int_to_string(value, str2, 10)) == - 0); - assert(strcmp(itoa(value, str1, 16), int_to_string(value, str2, 16)) == - 0); + // assert(strcmp(itoa(value, str1, 2), int_to_string(value, str2, 2)) == + // 0); + snprintf(str1, MAX_SIZE, "%o", value); + assert(strcmp(str1, int_to_string(value, str2, 8)) == 0); + snprintf(str1, MAX_SIZE, "%d", value); + assert(strcmp(str1, int_to_string(value, str2, 10)) == 0); + snprintf(str1, MAX_SIZE, "%x", value); + assert(strcmp(str1, int_to_string(value, str2, 16)) == 0); free(str1); free(str2); From 0f95bd92f9113ac4d434c2a563f28db8e151c081 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Fri, 31 Jul 2020 13:49:33 +0000 Subject: [PATCH 2/2] more docs --- conversions/int_to_string.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/conversions/int_to_string.c b/conversions/int_to_string.c index 3a56a04c0..01828ea80 100644 --- a/conversions/int_to_string.c +++ b/conversions/int_to_string.c @@ -1,4 +1,3 @@ - /** * @file * @brief Convert a positive integer to string (non-standard function) @@ -52,26 +51,26 @@ char *int_to_string(uint16_t value, char *dest, int base) static void test() { const int MAX_SIZE = 100; - for (int i = 1; i <= 100; ++i) - { - char *str1 = (char *)calloc(sizeof(char), MAX_SIZE); - char *str2 = (char *)calloc(sizeof(char), MAX_SIZE); + char *str1 = (char *)calloc(sizeof(char), MAX_SIZE); + char *str2 = (char *)calloc(sizeof(char), MAX_SIZE); + for (int i = 1; i <= 100; ++i) /* test 100 random numbers */ + { /* Generate value from 0 to 100 */ int value = rand() % 100; // assert(strcmp(itoa(value, str1, 2), int_to_string(value, str2, 2)) == // 0); - snprintf(str1, MAX_SIZE, "%o", value); + snprintf(str1, MAX_SIZE, "%o", value); //* standard C - to octal */ assert(strcmp(str1, int_to_string(value, str2, 8)) == 0); - snprintf(str1, MAX_SIZE, "%d", value); + snprintf(str1, MAX_SIZE, "%d", value); /* standard C - to decimal */ assert(strcmp(str1, int_to_string(value, str2, 10)) == 0); - snprintf(str1, MAX_SIZE, "%x", value); + snprintf(str1, MAX_SIZE, "%x", value); /* standard C - to hexadecimal */ assert(strcmp(str1, int_to_string(value, str2, 16)) == 0); - - free(str1); - free(str2); } + + free(str1); + free(str2); } /** Driver Code */