Skip to content

Commit 5fc2901

Browse files
Merge pull request #1 from gitpod-io/feature
replaced itoa with snprintf & fixed other docs
2 parents 313a980 + 0f95bd9 commit 5fc2901

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

conversions/int_to_string.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
21
/**
32
* @file
4-
* @brief Convert integer to string (non-standard function)
3+
* @brief Convert a positive integer to string (non-standard function)
4+
* representation.
55
*/
66
#include <assert.h>
7+
#include <inttypes.h>
78
#include <stdio.h>
89
#include <stdlib.h>
910
#include <string.h>
@@ -13,14 +14,15 @@
1314
* Converts an integer value to a null-terminated string using the specified
1415
* base and stores the result in the array given by str parameter.
1516
* @param value Value to be converted to a string.
16-
* @param dest Array in memory where to store the resulting null-terminated
17+
* @param dest pointer to array in memory to store the resulting null-terminated
1718
* string.
1819
* @param base Numerical base used to represent the value as a string, between 2
1920
* and 16, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.
2021
* @returns A pointer to the resulting null-terminated string, same as parameter
2122
* str.
23+
* @note The destination array must be pre-allocated by the calling function.
2224
*/
23-
char *int_to_string(int value, char dest[], int base)
25+
char *int_to_string(uint16_t value, char *dest, int base)
2426
{
2527
const char hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7',
2628
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
@@ -49,26 +51,26 @@ char *int_to_string(int value, char dest[], int base)
4951
static void test()
5052
{
5153
const int MAX_SIZE = 100;
52-
for (int i = 1; i <= 100; ++i)
53-
{
54-
char *str1 = (char *)calloc(sizeof(char), MAX_SIZE);
55-
char *str2 = (char *)calloc(sizeof(char), MAX_SIZE);
54+
char *str1 = (char *)calloc(sizeof(char), MAX_SIZE);
55+
char *str2 = (char *)calloc(sizeof(char), MAX_SIZE);
5656

57+
for (int i = 1; i <= 100; ++i) /* test 100 random numbers */
58+
{
5759
/* Generate value from 0 to 100 */
5860
int value = rand() % 100;
5961

60-
assert(strcmp(itoa(value, str1, 2), int_to_string(value, str2, 2)) ==
61-
0);
62-
assert(strcmp(itoa(value, str1, 8), int_to_string(value, str2, 8)) ==
63-
0);
64-
assert(strcmp(itoa(value, str1, 10), int_to_string(value, str2, 10)) ==
65-
0);
66-
assert(strcmp(itoa(value, str1, 16), int_to_string(value, str2, 16)) ==
67-
0);
68-
69-
free(str1);
70-
free(str2);
62+
// assert(strcmp(itoa(value, str1, 2), int_to_string(value, str2, 2)) ==
63+
// 0);
64+
snprintf(str1, MAX_SIZE, "%o", value); //* standard C - to octal */
65+
assert(strcmp(str1, int_to_string(value, str2, 8)) == 0);
66+
snprintf(str1, MAX_SIZE, "%d", value); /* standard C - to decimal */
67+
assert(strcmp(str1, int_to_string(value, str2, 10)) == 0);
68+
snprintf(str1, MAX_SIZE, "%x", value); /* standard C - to hexadecimal */
69+
assert(strcmp(str1, int_to_string(value, str2, 16)) == 0);
7170
}
71+
72+
free(str1);
73+
free(str2);
7274
}
7375

7476
/** Driver Code */

0 commit comments

Comments
 (0)