|
8 | 8 |
|
9 | 9 | #include <itoa.h>
|
10 | 10 |
|
11 |
| -#include <stdlib.h> |
| 11 | +#include <string> |
| 12 | +#include <stdexcept> |
| 13 | + |
| 14 | +#include <stdio.h> |
12 | 15 |
|
13 | 16 | /**************************************************************************************
|
14 | 17 | * FUNCTION IMPLEMENTATION
|
15 | 18 | **************************************************************************************/
|
16 | 19 |
|
17 |
| -#ifdef __cplusplus |
18 |
| -extern "C" { |
19 |
| -#endif |
20 |
| - |
21 |
| -char * itoa(int value, char *string, int radix) |
| 20 | +std::string radixToFmtString(int const radix) |
22 | 21 | {
|
23 |
| - return itoa(value, string, radix); |
| 22 | + if (radix == 8) return std::string("%o"); |
| 23 | + else if (radix == 10) return std::string("%d"); |
| 24 | + else if (radix == 16) return std::string("%X"); |
| 25 | + else throw std::runtime_error("Invalid radix."); |
24 | 26 | }
|
25 | 27 |
|
26 |
| -char * ltoa(long value, char *string, int radix) |
| 28 | +char * itoa(int value, char * str, int radix) |
27 | 29 | {
|
28 |
| - return ltoa(value, string, radix); |
| 30 | + sprintf(str, radixToFmtString(radix).c_str(), value); |
| 31 | + return str; |
29 | 32 | }
|
30 | 33 |
|
31 |
| -char * utoa(unsigned value, char *string, int radix) |
| 34 | +char * ltoa(long value, char * str, int radix) |
32 | 35 | {
|
33 |
| - return utoa(value, string, radix); |
| 36 | + sprintf(str, radixToFmtString(radix).c_str(), value); |
| 37 | + return str; |
34 | 38 | }
|
35 | 39 |
|
36 |
| -char * ultoa(unsigned long value, char *string, int radix) |
| 40 | +char * utoa(unsigned value, char *str, int radix) |
37 | 41 | {
|
38 |
| - return ultoa(value, string, radix); |
| 42 | + sprintf(str, radixToFmtString(radix).c_str(), value); |
| 43 | + return str; |
39 | 44 | }
|
40 | 45 |
|
41 |
| -#ifdef __cplusplus |
42 |
| -} // extern "C" |
43 |
| -#endif |
| 46 | +char * ultoa(unsigned long value, char * str, int radix) |
| 47 | +{ |
| 48 | + sprintf(str, radixToFmtString(radix).c_str(), value); |
| 49 | + return str; |
| 50 | +} |
0 commit comments