Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

AnsiString to int

Options
  • 25-02-2003 2:40pm
    #1
    Registered Users 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 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 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 Posts: 950 ✭✭✭jessy


    Thanks Hussey that worked fine:D


Advertisement