How to convert CString to integer and float?

17,159

The proper UNICODE-compliant way of doing it in MFC is the following:

CString sInt = _T("10");
int n = _ttoi(sInt);

CString sFloat = _T("10.1");
float f = _ttof(sFloat);

As David Heffernan mentioned: If your project configuration is UNICODE only and you don't use MBCS and do not have any plans to target old MS OSs like Window 98 you can use:

CStringW s = L"10";
int i = _wtoi(s); 

In C++11 you can use the following:

std::string sInt = "10";
int i = std::stoi(sInt);

std::string sFloat = "10.1";
double d = std::stod(sFloat);
Share:
17,159
AnasShoaib90
Author by

AnasShoaib90

I m learning computer science to solve world's problems.

Updated on June 04, 2022

Comments

  • AnasShoaib90
    AnasShoaib90 almost 2 years

    I am trying to convert CString to int and float but unable to find any C++ library function to get this done. please help.

  • David Heffernan
    David Heffernan over 8 years
    No it is not. The proper Unicode way to do things is not to use TCHAR. And it's never the right thing to use these C standard library functions that have broken by design error handling.
  • Andrew Komiagin
    Andrew Komiagin over 8 years
    I agree that error handling in C standard library functions looks ancient these days. But I still find this approach most simple and OK for the task.
  • David Heffernan
    David Heffernan over 8 years
    The voting here is pretty disappointing. Encouraging TCHAR in 2015 is bizarre. Not to mention the fact that you neglect to check for errors. How about you add the necessary error checking code and show how wonderful it looks. Just in case the asker wants to be able to tell the difference between atoi("2147483647") and atoi("2147483648").
  • IInspectable
    IInspectable over 8 years
    @DavidHeffernan: The question is asking about converting a CString. Using TCHARs, the _T macro, and the _t-variants of the CRT is the correct way to deal with this. Had the code used CStringW, it would be a different story. You are correct, though, that _ttoi and friends exhibit broken error reporting, and are best left behind.
  • David Heffernan
    David Heffernan over 8 years
    @IInspectable No, TCHAR is not the correct approach. The asker might be using TCHAR, but should not be doing so, unless support for Windows 98 is needed. Which I doubt. In the asker can leave that ancient past behind, then CStringW is what is needed.
  • IInspectable
    IInspectable over 8 years
    @DavidHeffernan: This answer addresses the question that was asked, and is correct. There may be reasons why the OP cannot use CStringW (they might be maintaining ancient code that cannot be changed, or their W-key is broken on their machine). They might even be maintaining code that cannot be compiled as Unicode. If you think the question is wrong, comment on the question. This answer is not to blame.
  • David Heffernan
    David Heffernan over 8 years
    @IInspectable I think it's good for us to advise the users of these wider issues, rather than letting them think that TCHAR is still the way to go.
  • Barmak Shemirani
    Barmak Shemirani over 8 years
    I agree about the useless complexity of TCHAR But std::stod is the worst, it needs try/catch otherwise in release mode it quietly kills the program if parameter is wrong. I don't know why they make it like that. _wtoi or wcstol work as expected.
  • IInspectable
    IInspectable over 7 years
    @BarmakShemirani: std::stod doesn't need try/catch handlers. It reports errors by way of exceptions. If the exception isn't caught, the program dies as quietly or loudly as your last call to std::set_unexpected requested.
  • Barmak Shemirani
    Barmak Shemirani over 7 years
    @IInspectable thanks, I didn't know about std::set_unexpected