|
1 |
| - |
2 | 1 | /**
|
3 | 2 | * @file
|
4 |
| - * @brief Convert integer to string (non-standard function) |
| 3 | + * @brief Convert a positive integer to string (non-standard function) |
| 4 | + * representation. |
5 | 5 | */
|
6 | 6 | #include <assert.h>
|
| 7 | +#include <inttypes.h> |
7 | 8 | #include <stdio.h>
|
8 | 9 | #include <stdlib.h>
|
9 | 10 | #include <string.h>
|
|
13 | 14 | * Converts an integer value to a null-terminated string using the specified
|
14 | 15 | * base and stores the result in the array given by str parameter.
|
15 | 16 | * @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 |
17 | 18 | * string.
|
18 | 19 | * @param base Numerical base used to represent the value as a string, between 2
|
19 | 20 | * and 16, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.
|
20 | 21 | * @returns A pointer to the resulting null-terminated string, same as parameter
|
21 | 22 | * str.
|
| 23 | + * @note The destination array must be pre-allocated by the calling function. |
22 | 24 | */
|
23 |
| -char *int_to_string(int value, char dest[], int base) |
| 25 | +char *int_to_string(uint16_t value, char *dest, int base) |
24 | 26 | {
|
25 | 27 | const char hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7',
|
26 | 28 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
@@ -49,26 +51,26 @@ char *int_to_string(int value, char dest[], int base)
|
49 | 51 | static void test()
|
50 | 52 | {
|
51 | 53 | 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); |
56 | 56 |
|
| 57 | + for (int i = 1; i <= 100; ++i) /* test 100 random numbers */ |
| 58 | + { |
57 | 59 | /* Generate value from 0 to 100 */
|
58 | 60 | int value = rand() % 100;
|
59 | 61 |
|
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); |
71 | 70 | }
|
| 71 | + |
| 72 | + free(str1); |
| 73 | + free(str2); |
72 | 74 | }
|
73 | 75 |
|
74 | 76 | /** Driver Code */
|
|
0 commit comments