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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

AnsiString to int

  • 25-02-2003 1: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