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 <string> // for string
19 using namespace std;
21 string itos(int n)
22 {
23 int sign;
24 string s;
26 if ((sign = n) < 0) // record sign
27 n = -n; // make n positive
28 do { // generate digits in reverse order
29 s += (char(n % 10) + '0'); // get next digit
30 } while ((n/=10) > 0); // delete it
32 if (sign < 0)
33 s += '-';
35 reverse(s.begin(), s.end()); // This is what the code should look like
36 // if the string class is compatible with
37 // the standard C++ string class
38 #ifdef DUMB_OS_LIKE_WINDOWS
39 // In Windows, we'll use this hack...
40 for (int i=0, j=s.GetLength()-1; i<j; i++, j--)
41 {
42 char c = s[i];
43 // s[i] = s[j];
44 // s[j] = c;
45 s.SetAt(i, s[j]);
46 s.SetAt(j, c);
47 }
48 #endif
50 return s;
51 }
53 string ultos(unsigned long n)
54 {
55 string s;
57 do { // generate digits in reverse order
58 s += (char(n % 10) + '0'); // get next digit
59 } while ((n/=10) > 0); // delete it
61 reverse(s.begin(), s.end()); // This is what the code should look like
62 // if the string class is compatible with
63 // the standard C++ string class
64 #ifdef DUMB_OS_LIKE_WINDOWS
65 // In Windows, we'll use this hack...
66 for (int i=0, j=s.GetLength()-1; i<j; i++, j--)
67 {
68 char c = s[i];
69 // s[i] = s[j];
70 // s[j] = c;
71 s.SetAt(i, s[j]);
72 s.SetAt(j, c);
73 }
74 #endif
76 return s;
77 }