-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathultoa.cpp
88 lines (68 loc) · 1.84 KB
/
ultoa.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
/**
* C++ version 0.4 std::string style "itoa":
* Contributions from Stuart Lowe, Ray-Yuan Sheu,
* Rodrigo de Salvo Braz, Luc Gallant, John Maloney
* and Brian Hunt
*/
template <class T>
std::string __toa(T value, int base) {
std::string buf;
// check that the base if valid
if (base < 2 || base > 16) return buf;
enum { kMaxDigits = 35 };
buf.reserve( kMaxDigits ); // Pre-allocate enough space.
int quotient = value;
// Translating number to string with base:
do {
buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
quotient /= base;
} while ( quotient );
// Append the negative sign
if ( value < 0) buf += '-';
std::reverse( buf.begin(), buf.end() );
return buf;
}
template <class T>
std::string __utoa(T value, int base) {
std::string buf;
// check that the base if valid
if (base < 2 || base > 16) return buf;
enum { kMaxDigits = 35 };
buf.reserve( kMaxDigits ); // Pre-allocate enough space.
int quotient = value;
// Translating number to string with base:
do {
buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
quotient /= base;
} while ( quotient );
std::reverse( buf.begin(), buf.end() );
return buf;
}
char* ultoa( unsigned long __val, char* __s, int __radix )
{
std::string result = __utoa(__val,__radix);
strcpy(__s,result.c_str());
return __s;
}
char* ltoa( long __val, char* __s, int __radix )
{
std::string result = __toa(__val,__radix);
strcpy(__s,result.c_str());
return __s;
}
char* itoa( int __val, char* __s, int __radix )
{
std::string result = __toa(__val,__radix);
strcpy(__s,result.c_str());
return __s;
}
char* utoa( unsigned int __val, char* __s, int __radix )
{
std::string result = __utoa(__val,__radix);
strcpy(__s,result.c_str());
return __s;
}