4
// From: http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
5
// John Maloney has pointed out various problems with the previous implementation.
6
// One of the major issues was the amount of heap allocation going on.
7
// He suggested that a lot of this be removed to speed up the algorithm.
8
// Below are two versions based on his excellent suggestions. The char* version
9
// is at least 10 times faster than the code above. The new std::string version
10
// is 3 times faster than before. Although the char* version is faster, you should
11
// check that you have allocated enough space to hold the output.
13
// C++ version std::string style "itoa":
14
tstring IntegerToChar(int value, int base)
16
enum { kMaxDigits = 35 };
18
buf.reserve( kMaxDigits ); // Pre-allocate enough space.
19
// check that the base if valid
20
if (base < 2 || base > 16)
23
// Translating number to string with base:
26
buf += TEXT("0123456789abcdef")[ std::abs( quotient % base ) ];
29
// Append the negative sign for base 10
30
if ( value < 0 && base == 10) buf += TEXT('-');
31
std::reverse( buf.begin(), buf.end() );
35
// C++ version char* style "itoa":
36
TCHAR* IntegerToChar( int value, TCHAR* result, int base )
38
// check that the base if valid
39
if (base < 2 || base > 16)
48
*out = TEXT("0123456789abcdef")[ std::abs( quotient % base ) ];
52
// Only apply negative sign for base 10
53
if ( value < 0 && base == 10) *out++ = TEXT('-');
55
std::reverse( result, out );
60
// convert an hexadecimal string to t_u32
61
t_u32 HexCharToInteger(const TCHAR* s)
63
int n = 0; // position in string
64
int m = 0; // position in digit[] to shift
65
int count; // loop index
66
unsigned long intValue = 0; // integer value of hex string
67
int digit[16]; // hold values to convert
69
const TCHAR *hexStg = s;
70
if((s[0] == TEXT('0')) && ((s[1] == TEXT('X')) || (s[1] == TEXT('x'))))
77
if (hexStg[n] == TEXT('\0'))
79
if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
80
digit[n] = hexStg[n] & 0x0f; //convert to int
81
else if (hexStg[n] >= TEXT('a') && hexStg[n] <= TEXT('f')) //if a to f
82
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
83
else if (hexStg[n] >= TEXT('A') && hexStg[n] <= TEXT('F')) //if A to F
84
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
93
// digit[n] is value of hex digit at position n
94
// (m << 2) is the number of positions to shift
95
// OR the bits into return value
96
intValue = intValue | (digit[n] << (m << 2));
97
m--; // adjust the position to set
98
n++; // next digit to process
103
int CharToInteger(const TCHAR* digit)
106
if(CharToInteger(digit, i))
114
BOOL CharToInteger(const TCHAR* digit, INT& ret)
119
//--- Convert each digit char and add into result.
121
if(digit[i] == TEXT('-'))
126
else if(digit[i] == TEXT('+'))
132
while(digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
134
result = (result * 10) + (digit[i] - TEXT('0'));
138
if( digit[i] == TEXT('.')) // test for '.' in case the characters represent a double or a float value
141
while(digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
147
const char* hello = "HelloWorld";
148
bool null_char = (digit[i] == 0);
149
bool space_char = (digit[i] == TEXT(' '));
150
bool tab_char = (digit[i] == TEXT('\t'));
151
bool win_end_of_line = (digit[i] == TEXT('\r')) && (digit[i+1] == TEXT('\n'));
152
bool line_feed = (digit[i] == ('\n'));
153
bool carriage_return = (digit[i] == ('\r'));
154
//--- Check that there were no non-digits at end.
155
if((!null_char) && (!tab_char) && (!space_char) && (!win_end_of_line) && (!line_feed) && (!carriage_return))
157
nuxDebugMsg(TEXT("[CharToInteger]: Unexpected character at the end of number %d in string: %s"), sign*result, digit);
163
double CharToDouble(const TCHAR* digit)
166
if(CharToDouble(digit, d))
172
BOOL CharToDouble(const TCHAR* digit, double& ret)
180
if (digit[i] == TEXT('-'))
185
else if (digit[i] == TEXT('+'))
191
//--- get integer portion
192
while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
194
result = (result * 10) + digit[i] - TEXT('0');
198
//--- get decimal point and fraction, if any.
199
if (digit[i] == TEXT('.'))
203
while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
205
result += (digit[i] - TEXT('0')) * scale;
211
//--- error if we're not at the end of the number
213
&& (digit[i] != TEXT(' '))
214
&& (digit[i] != TEXT('\t'))
216
// end of line on Windows
217
&& (digit[i] != TEXT('\r'))
218
&& (digit[i+1] != TEXT('\n'))
220
// end of line on Unix.
221
&& (digit[i] != TEXT('\n'))
225
// not NULL terminated or unexpected character;
233
float CharToFloat(const TCHAR* digit)
236
if(CharToFloat(digit, f))
242
BOOL CharToFloat(const TCHAR* digit, float& ret)
250
if (digit[i] == TEXT('-'))
255
else if (digit[i] == TEXT('+'))
261
//--- get integer portion
262
while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
264
result = (result * 10) + digit[i] - TEXT('0');
268
//--- get decimal point and fraction, if any.
269
if (digit[i] == TEXT('.'))
273
while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
275
result += (digit[i] - TEXT('0')) * scale;
281
//--- error if we're not at the end of the number
283
&& (digit[i] != TEXT(' '))
284
&& (digit[i] != TEXT('\t'))
286
// end of line on Windows
287
&& (digit[i] != TEXT('\r'))
288
&& (digit[i+1] != TEXT('\n'))
290
// end of line on Unix.
291
&& (digit[i] != TEXT('\n'))
295
// not NULL terminated or unexpected character;