How to convert a CString
object to integer in MFC.
相关问题
- how to call a C++ dll from C# windows application
- efficiently calling unmanaged method taking unmana
- Why is my COM factory never released during the pr
- Create CFrameWnd gives first-chance exceptions--wh
- Underline is drawn outside of rectangle reported b
相关文章
- C++: Callback typedefs with __stdcall in MSVC
- Is it possible to check whether you are building f
- Which VC++ redistributable package to choose (x86
- MFC CListView响应HDN_ITEMCHANGING后改变列宽无法自动显示隐藏水平滚动条
- How can I handle the Return key in a CEdit control
- How can I create a guid in MFC
- How to convert Byte Array to hex string in visual
- Visual Studio unable to recognise my MFC library f
A
_ttoi
function can convertCString
to integer, both wide char and ansi char can work. Below is the details:The simplest approach is to use the
atoi()
function found instdlib.h
: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:you can also use good old sscanf.
You may use the C atoi function ( in a try / catch clause because the conversion isn't always possible) But there's nothing in the MFC classes to do it better.
The problem with the accepted answer is that it cannot signal failure. There's
strtol
(STRing TO Long) which can. It's part of a larger family:wcstol
(Wide Character String TO Long, e.g. Unicode),strtoull
(TO Unsigned Long Long, 64bits+),wcstoull
,strtof
(TO Float) andwcstof
.If you are using
TCHAR.H
routine (implicitly, or explicitly), be sure you use_ttoi()
function, so that it compiles for both Unicode and ANSI compilations.More details: https://msdn.microsoft.com/en-us/library/yd5xkb5c.aspx