Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Converting a long to a string?

    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:

    Code:
    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...
    Last edited by jkd; 10-19-2002 at 02:14 AM.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	long number;
    	char string[50];
    
    	number=102202;
    	sprintf(string,"%d",number);
    	printf("%s",string);
    	return 0;
    }
    I hope this will help you a bit

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    *sigh* C.... I guess it'll work, though I've been trying to shy away from the ugly C functions in C++...

    Thanks though.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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?

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Now I have something like:

    Code:
    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?

  • #6
    New Coder
    Join Date
    Oct 2002
    Location
    middle of nowhere
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    }
    else
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •