How2 convert long to sring in C++ ?

 Home

News

Forums

Hardware

CPUs

Motherboards

Video

Guides

CPU Prices

RAM Prices


Sharky Extreme : Forums:


Results 1 to 8 of 8

Thread: How2 convert long to sring in C++ ?

  1. #1
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Phila, PA
    Posts
    519

    Post How2 convert long to sring in C++ ?

    How2 convert long to sring in C++ ?
    long A = 12345678;
    string B =A; // < won't compile obviously.

    Does any1 know a function that converts long to string ?

    Thanks.

    ------------------
    There are no stupid questions, there are stupid answers.
    There are no stupid questions, there are stupid answers.

  2. #2
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    Originally posted by IL96:
    How2 convert long to sring in C++ ?
    long A = 12345678;
    string B =A; // < won't compile obviously.

    Does any1 know a function that converts long to string ?

    Thanks.

    string B = (string) A;

    ------------------
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3

    uwcdc.com or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  3. #3
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Phila, PA
    Posts
    519

    Post

    Originally posted by namgor:
    string B = (string) A;

    No, you see A is not a string, it's long.
    Anywhoo, i think i found a function taht sorta kinda does that, it works like this (for future reference)

    int radix = 10; // base (2,8,10,16, whatever)
    char buffer[33];// temp buffer
    char *strptr;// pointer to an aray of characters (string)
    strptr = ltoa(12345678,buffer,radix);
    printf("The value is: %s\n",strptr);

    ------------------
    There are no stupid questions, there are stupid answers.

    [This message has been edited by IL96 (edited July 16, 2001).]
    There are no stupid questions, there are stupid answers.

  4. #4
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Post

    What he did was called a type cast.

    string B = (string) A;

    What this does is that it turns A into a string and then assigns its value to B.

    Check in a book or online for 'Type Casts' or just 'Casts'

    Namgor knows his stuff


    ------------------
    ## root is the greed of all evil ##

    /* Navi Specs */
    • Abit KT7-RAID
    • Duron 800 @ 1GHz
    • Geforce 2 GTS 32MB DDR
    • 512MB Micron/Crucial SDRAM
    • SB Live! 5.1 w/ Live! Drive
    • Maxtor ATA100 40GB
    • Pioneer 16x DVD
    • HP 9300 series CD-RW
    • Netgear FA311 NIC
    • Win2k Server / Linux Mandrake 8.0
    • Sony Multiscan 17SF
    ## root is the greed of all evil ##

  5. #5
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    Wow, you can cast a long into a string? Man, this C++ stuff never ceases to amaze me!

  6. #6
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    I know there is another to cast it, (called dynamic cast??) Anyway, I believe this method can do the trick so i didnt bother look up the other method.

    ------------------
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3

    uwcdc.com or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  7. #7
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    string a = (string)an_int; does not work because std::string does not provide a conversion operator of type int


    try this

    #include <iostream>
    #include <sstream>
    #include <string>

    int main()
    {
    int i = 123;
    std::stringstream buf;
    buf<<i;

    std::string thestring = buf.str();

    std::cout << thestring.c_str();
    return 0;
    }

    sorry my first version of code was incorrect because std::string does not provide any overloaded operators for conversion so what you need is a string stream

    and dynamic_cast is for casting between hirarchily related objects (eg. base to derived) using runtime type identification so it has no use here

    BTW the includes dont have .h in them because that is the C++ style and puts the classes in the namespace "std" hence the use of std::

    [This message has been edited by dighn (edited July 17, 2001).]
    .

  8. #8
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Post

    Thank you for the correction.

    I'm just an amateur C programmer. So I only know the basics of casts and such. I don't really use them much except once in a while when I have to for functions I write or what not.

    Thanks again, learn something new everyday.

    [This message has been edited by biosx (edited July 17, 2001).]
    ## root is the greed of all evil ##

Posting Permissions

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