Discussion:
Convert CString to int
(too old to reply)
Matthias Pospiech
2007-04-17 12:48:14 UTC
Permalink
I have the simple problem to do something like

CString PhaseText;
m_WndEditPhase.GetWindowTextW(PhaseText);
int phase = ConvertCStringToInt(PhaseText);

but I do not know how the ConvertCStringToInt should look like in real code.

atoi(PhaseText.GetBuffer(0));
does not work because of conversion problems and seems to be C-code.

Solution ?

Matthias
Arman Sahakyan
2007-04-17 13:10:00 UTC
Permalink
Post by Matthias Pospiech
I have the simple problem to do something like
CString PhaseText;
m_WndEditPhase.GetWindowTextW(PhaseText);
int phase = ConvertCStringToInt(PhaseText);
but I do not know how the ConvertCStringToInt should look like in real code.
atoi(PhaseText.GetBuffer(0));
does not work because of conversion problems and seems to be C-code.
Solution ?
Matthias
Use _tcstoi to have support in both unicode and non-unicode environments.
But this will not solve your problem. You should do smth like this;
int n = _tcstoi(PhaseText, NULL, 10); // 10 is base

Or an object-oriented approach;
#include <sstream>
std::stringstream ss;
ss << PhaseText;
int n;
ss >> n;
--
======
Arman
David Wilkinson
2007-04-17 14:21:25 UTC
Permalink
Post by Matthias Pospiech
I have the simple problem to do something like
CString PhaseText;
m_WndEditPhase.GetWindowTextW(PhaseText);
int phase = ConvertCStringToInt(PhaseText);
but I do not know how the ConvertCStringToInt should look like in real code.
atoi(PhaseText.GetBuffer(0));
does not work because of conversion problems and seems to be C-code.
Matthias:

int phase = _ttoi(PhaseText);
--
David Wilkinson
Visual C++ MVP
Joseph M. Newcomer
2007-04-22 20:49:56 UTC
Permalink
_ttoi. No need for GetBuffer at all.

CString s;
...GetWindowText(s);
int n = _ttoi(s);
joe
Post by Matthias Pospiech
I have the simple problem to do something like
CString PhaseText;
m_WndEditPhase.GetWindowTextW(PhaseText);
int phase = ConvertCStringToInt(PhaseText);
but I do not know how the ConvertCStringToInt should look like in real code.
atoi(PhaseText.GetBuffer(0));
does not work because of conversion problems and seems to be C-code.
Solution ?
Matthias
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Continue reading on narkive:
Loading...