Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

AnsiString to int

  • 25-02-2003 02:40PM
    #1
    Registered Users, Registered Users 2 Posts: 950 ✭✭✭


    hello all

    I need to be able to convert an AnsiString to an int in c++;

    dose anyone know how to do this and if so could they please give a code example.

    thanks in advance.


Comments

  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    You can use the C libraries in C++ so
    int atoi(const char*)
    
    is available.

    You can either use the header <stdlib> or <cstdlib> which merely includes <stdlib> but puts it all in the std namespace (where it belongs).

    With streams >> is overloaded to take int& arguments, so if the use of this conversion is with a string from a stream you can just use that instead.


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    This will work for standard strings in c/c++ but
    ansiStrings are different as they are managed
    by c++ so it will not work;
    maybe if i know how to convert AnsiString to String then i
    could used the atoi function;
    :confused:


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    I haven't tried this myself but

    convert the ansistring to char *
    and then use the atoi function??

    eg.
    
    int convert_ansi_int(AnsiString str) 
    {
    
    return atoi(str.c_str());
    
    }
    
    // this is equililebt of this (  I think)
    int convert_ansi_int(AnsiString str) 
    {
    
    char * tempStr = str.c_str();
    int num = atoi(tempStr);
    return num;
    
    }
    


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Thanks Hussey that worked fine:D


Advertisement