Skip to content

Commit d71e601

Browse files
Merge pull request #586 from shellhub/feature
Convert integer to string
2 parents 6d4e936 + aded2a6 commit d71e601

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [Decimal To Octal](https://github.com/TheAlgorithms/C/blob/master/conversions/decimal_to_octal.c)
1818
* [Decimal To Octal Recursion](https://github.com/TheAlgorithms/C/blob/master/conversions/decimal_to_octal_recursion.c)
1919
* [Hexadecimal To Octal](https://github.com/TheAlgorithms/C/blob/master/conversions/hexadecimal_to_octal.c)
20+
* [Int To String](https://github.com/TheAlgorithms/C/blob/master/conversions/int_to_string.c)
2021
* [Octal To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_decimal.c)
2122
* [To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/to_decimal.c)
2223

conversions/int_to_string.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @file
3+
* @brief Convert a positive integer to string (non-standard function)
4+
* representation.
5+
*/
6+
#include <assert.h>
7+
#include <inttypes.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
#include <time.h>
12+
13+
/**
14+
* Converts an integer value to a null-terminated string using the specified
15+
* base and stores the result in the array given by str parameter.
16+
* @param value Value to be converted to a string.
17+
* @param dest pointer to array in memory to store the resulting null-terminated
18+
* string.
19+
* @param base Numerical base used to represent the value as a string, between 2
20+
* and 16, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.
21+
* @returns A pointer to the resulting null-terminated string, same as parameter
22+
* str.
23+
* @note The destination array must be pre-allocated by the calling function.
24+
*/
25+
char *int_to_string(uint16_t value, char *dest, int base)
26+
{
27+
const char hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7',
28+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
29+
30+
int len = 0;
31+
do
32+
{
33+
dest[len++] = hex_table[value % base];
34+
value /= base;
35+
} while (value != 0);
36+
37+
/* reverse characters */
38+
for (int i = 0, limit = len / 2; i < limit; ++i)
39+
{
40+
char t = dest[i];
41+
dest[i] = dest[len - 1 - i];
42+
dest[len - 1 - i] = t;
43+
}
44+
dest[len] = '\0';
45+
return dest;
46+
}
47+
48+
/** Test function
49+
* @returns `void`
50+
*/
51+
static void test()
52+
{
53+
const int MAX_SIZE = 100;
54+
char *str1 = (char *)calloc(sizeof(char), MAX_SIZE);
55+
char *str2 = (char *)calloc(sizeof(char), MAX_SIZE);
56+
57+
for (int i = 1; i <= 100; ++i) /* test 100 random numbers */
58+
{
59+
/* Generate value from 0 to 100 */
60+
int value = rand() % 100;
61+
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);
70+
}
71+
72+
free(str1);
73+
free(str2);
74+
}
75+
76+
/** Driver Code */
77+
int main()
78+
{
79+
/* Intializes random number generator */
80+
srand(time(NULL));
81+
test();
82+
return 0;
83+
}

0 commit comments

Comments
 (0)