Convert MFC CString to integer

后端 未结 11 2125
南旧
南旧 2020-12-08 13:15

How to convert a CString object to integer in MFC.

11条回答
  •  孤城傲影
    2020-12-08 14:08

    The simplest approach is to use the atoi() function found in stdlib.h:

    CString s = "123";
    int x = atoi( s );
    

    However, this does not deal well with the case where the string does not contain a valid integer, in which case you should investigate the strtol() function:

    CString s = "12zzz";    // bad integer
    char * p;
    int x = strtol ( s, & p, 10 );
    if ( * p != 0 ) {
       // s does not contain an integer
    }
    

提交回复
热议问题