-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtoa.h
63 lines (54 loc) · 1.43 KB
/
xtoa.h
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
#ifndef INCLUDED_BOGODUINO_XTOA_H
#define INCLUDED_BOGODUINO_XTOA_H
#include <cstdio>
inline char *dtostrf(float value, int size, int prec, char *buf)
{
snprintf(buf, 20, "%*.*f", size, prec, value);
return buf;
}
inline char *dtostrf(double value, int size, int prec, char *buf)
{
snprintf(buf, 20, "%*.*lf", size, prec, value);
return buf;
}
template<class T> inline void xtoa(const char *fmt, T value, char *buf)
{
snprintf(buf, 30, fmt, value);
}
inline void utoa(unsigned value, char* buf, int base)
{
switch (base) {
case 8: xtoa("%o", value, buf); break;
case 10: xtoa("%u", value, buf); break;
case 16: xtoa("%x", value, buf); break;
default: abort();
}
}
inline void itoa(int value, char* buf, int base)
{
switch (base) {
case 8: xtoa("%o", value, buf); break;
case 10: xtoa("%d", value, buf); break;
case 16: xtoa("%x", value, buf); break;
default: abort();
}
}
inline void ltoa(long value, char* buf, int base)
{
switch (base) {
case 8: xtoa("%lo", value, buf); break;
case 10: xtoa("%ld", value, buf); break;
case 16: xtoa("%lx", value, buf); break;
default: abort();
}
}
inline void ultoa(unsigned long value, char* buf, int base)
{
switch (base) {
case 8: xtoa("%lo", value, buf); break;
case 10: xtoa("%lu", value, buf); break;
case 16: xtoa("%lx", value, buf); break;
default: abort();
}
}
#endif //INCLUDED_BOGODUINO_XTOA_H