3

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

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
AnasShoaib90
  • 629
  • 2
  • 7
  • 14

1 Answers1

6

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);
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • 1
    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. – David Heffernan Dec 11 '15 at 11:27
  • 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. – Andrew Komiagin Dec 11 '15 at 11:29
  • 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")`. – David Heffernan Dec 11 '15 at 11:45
  • @DavidHeffernan: The question is asking about converting a `CString`. Using `TCHAR`s, 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. – IInspectable Dec 11 '15 at 11:57
  • @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. – David Heffernan Dec 11 '15 at 11:58
  • @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. – IInspectable Dec 11 '15 at 12:05
  • @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. – David Heffernan Dec 11 '15 at 12:07
  • 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. – Barmak Shemirani Dec 11 '15 at 17:57
  • @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](http://en.cppreference.com/w/cpp/error/set_unexpected) requested. – IInspectable Oct 24 '16 at 21:24
  • @IInspectable thanks, I didn't know about `std::set_unexpected` – Barmak Shemirani Oct 24 '16 at 23:07