1 /////////////////////////////////////////////////////////////////////////
2 // ftoa.cpp
3 //
4 // Copyright (c) 1996-2003 Bryce W. Harrington [bryce at osdl dot org]
5 //
6 //-----------------------------------------------------------------------
7 // License: This code may be used by anyone for any purpose
8 // so long as the copyright notices and this license
9 // statement remains attached.
10 //-----------------------------------------------------------------------
11 //
12 // This routine converts an integer into a string
13 //
14 /////////////////////////////////////////////////////////////////////////
16 // Standard include files
17 #include <algorithm>
18 #include <string> // for string
20 using namespace std;
22 string itos(int n)
23 {
24 int sign;
25 string s;
27 if ((sign = n) < 0) // record sign
28 n = -n; // make n positive
29 do { // generate digits in reverse order
30 s += (char(n % 10) + '0'); // get next digit
31 } while ((n/=10) > 0); // delete it
33 if (sign < 0)
34 s += '-';
36 reverse(s.begin(), s.end()); // This is what the code should look like
37 // if the string class is compatible with
38 // the standard C++ string class
39 #ifdef DUMB_OS_LIKE_WINDOWS
40 // In Windows, we'll use this hack...
41 for (int i=0, j=s.GetLength()-1; i<j; i++, j--)
42 {
43 char c = s[i];
44 // s[i] = s[j];
45 // s[j] = c;
46 s.SetAt(i, s[j]);
47 s.SetAt(j, c);
48 }
49 #endif
51 return s;
52 }
54 string ultos(unsigned long n)
55 {
56 string s;
58 do { // generate digits in reverse order
59 s += (char(n % 10) + '0'); // get next digit
60 } while ((n/=10) > 0); // delete it
62 reverse(s.begin(), s.end()); // This is what the code should look like
63 // if the string class is compatible with
64 // the standard C++ string class
65 #ifdef DUMB_OS_LIKE_WINDOWS
66 // In Windows, we'll use this hack...
67 for (int i=0, j=s.GetLength()-1; i<j; i++, j--)
68 {
69 char c = s[i];
70 // s[i] = s[j];
71 // s[j] = c;
72 s.SetAt(i, s[j]);
73 s.SetAt(j, c);
74 }
75 #endif
77 return s;
78 }