View Full Version : Converting a long to a string?

Oct 19th, 2002, 12:23 AM
I'm using the apstring class in C++, and am writing a toString() method for a Rational number class.

Not thinking due to years of experience in loosely-typed scripting languages, I have something like:

apstring Rational::toString() const {
return numer + "/" + denom;

Naturally assuming that numer and denom (both longs) will be implicitly typecasted to an apstring object. But of course, the compiler complains instead.

Thinking about this, apstring(long) defines an apstring with that length. But I am unaware of a way to convert it into a string. i.e.:
123 -> "123"

I suppose I could keep dividing by powers of ten, typecast to an int to lose decimals, add 48 ('1' is 49 in ASCII), and typecast to a char, but that is way too much effort for this.

And I don't suppose there is a toString() method of the long datatype, considering this isn't Java...

Oct 19th, 2002, 11:15 AM
I don't realy understand your post (but that is my fault ;) )
but if you want to convert a long to a string, you can allways do it like this:

#include <stdio.h>

int main(void)
long number;
char string[50];

return 0;

I hope this will help you a bit

Oct 19th, 2002, 07:53 PM
*sigh* C.... I guess it'll work, though I've been trying to shy away from the ugly C functions in C++...

Thanks though. :)

Oct 19th, 2002, 08:52 PM
Actually, this doesn't work now that I tried it.

Also, not limiting myself to a fixed-length string is one the reasons I am using apstring instead of cstrings....

Surely there must be a toString() or equivalent method *somewhere* for long datatypes?

Perhaps I could do something like:

ostream os;
os << numer << '/' << denom;

And somehow read the contents of os back into a string?

Oct 19th, 2002, 11:04 PM
Now I have something like:

apstring Rational::toString() const {
stringstream sstream(stringstream::in | stringstream::out);

sstream << numer << '/' << denom;

// sstream.str() is the string I want
// but I cannot typecast directly to
// apstring

return ?????;

Any ideas? Or perhaps a better method?

Shawn Curry
Oct 22nd, 2002, 08:29 AM
I'm kinda new at this, but I'm writing that very library. I set up a class as a pointer to a string(for flexible length) and and unsigned long to hold the strlen() so i wasnt calling it every 30 sec. You're right about dividing by powers of ten, but this is grossly inefficient. If there's a way to accept the input as a string in the first place, you dont have to divide so much. You can break it down easily by powers of ten by just adding and subtracting zeros. Here's my conversion from a string to an unsigned long:

unsigned long Whole::convert(Whole& n)
unsigned long len = n.length;
unsigned long total = convertchar(n.number[--len]);
unsigned long weight = 10;

while(len > 0)
total = (total + ( (convertchar(n.number[--len])) * weight ) );
weight *= 10;
return total;

Whole is my class(pointer to char and unsigned int length)
here's the convertchar:
int Whole::convertchar(char in)
//std::cout << "\nchar convert(int) passed: " << in;
int output = (in - 48);
if(output < 0)
std::cout << "\nERROR! int convert(char) passed: " << output;
return 1;
return output;

an unsigned long can only hold up to about 3,000,000,000 I think so the most it ever really multiplies is ten times. Hope that helps.