Skip to content

Commit 23c32e1

Browse files
authored
Merge pull request #41 from meshtastic/fix-utoa-crash
Fix crash on startup in itoa.cpp
2 parents b14bccb + 2797629 commit 23c32e1

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

cores/portduino/itoa.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
#include "itoa.h"
2-
#include <stdlib.h>
1+
#include <itoa.h>
2+
#include <string>
3+
#include <stdexcept>
4+
#include <stdio.h>
35

4-
// FIXME - implement this
56

6-
char *itoa(int value, char *string, int radix) {
7-
return NULL;
7+
std::string radixToFmtString(int const radix)
8+
{
9+
if (radix == 8) return std::string("%o");
10+
else if (radix == 10) return std::string("%d");
11+
else if (radix == 16) return std::string("%X");
12+
else throw std::runtime_error("Invalid radix.");
813
}
914

10-
char *ltoa(long value, char *string, int radix) {
11-
return NULL;
15+
char * itoa(int value, char * str, int radix)
16+
{
17+
sprintf(str, radixToFmtString(radix).c_str(), value);
18+
return str;
1219
}
1320

14-
char *utoa(unsigned value, char *string, int radix) {
15-
return NULL;
21+
char * ltoa(long value, char * str, int radix)
22+
{
23+
sprintf(str, radixToFmtString(radix).c_str(), value);
24+
return str;
1625
}
1726

18-
char *ultoa(unsigned long value, char *string, int radix) {
19-
return NULL;
20-
}
27+
char * utoa(unsigned value, char *str, int radix)
28+
{
29+
sprintf(str, radixToFmtString(radix).c_str(), value);
30+
return str;
31+
}
32+
33+
char * ultoa(unsigned long value, char * str, int radix)
34+
{
35+
sprintf(str, radixToFmtString(radix).c_str(), value);
36+
return str;
37+
}

0 commit comments

Comments
 (0)