-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathitoa.cpp
64 lines (54 loc) · 1.75 KB
/
itoa.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright (c) 2020 Arduino. All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <api/itoa.h>
#include <string>
#include <stdexcept>
#include <stdio.h>
/**************************************************************************************
* FUNCTION IMPLEMENTATION
**************************************************************************************/
std::string radixToFmtString(int const radix)
{
if (radix == 8) return std::string("%o");
else if (radix == 10) return std::string("%d");
else if (radix == 16) return std::string("%X");
else throw std::runtime_error("Invalid radix.");
}
char * itoa(int value, char * str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
return str;
}
char * ltoa(long value, char * str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
return str;
}
char * utoa(unsigned value, char *str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
return str;
}
char * ultoa(unsigned long value, char * str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
return str;
}