6

I would like to display an int value in a win32 MessageBox. I have read some different methods to perform this cast. Can someone provide me with a good implementation.

New to Win32 programming so go easy :)

update

So this is what i have so far. It works.. but the text looks like Chinese or some other double byte characters. I am not groking the Unicode vs. not Unicode types. Can someone help me understand where I am going wrong?

 int volumeLevel = 6;
 std::stringstream os;
 os<<volumeLevel;
 std::string intString = os.str();  
  MessageBox(plugin.hwndParent,(LPCTSTR)intString.c_str(), L"", MB_OK);
Nick
  • 19,198
  • 51
  • 185
  • 312
  • 1
    Possible duplicate of [Formatting an integer in C++](http://stackoverflow.com/questions/2815746/formatting-an-integer-in-c). (Use `wostringstream` for Unicode.) – In silico Aug 25 '11 at 03:17
  • 1
    @In silico: Nick is asking about converting an int to LPCTSTR, you are guiding him to use the wchar_t based wostringstream. Which assumes his project is compiled in with UNICODE or _UNICODE enabled. – Jack Aug 25 '11 at 03:22
  • To account for both unicode and multi-byte encodings (as `LPC*T*STR` suggests), you'll have to use `#ifdef UNICODE typedef wostringstream tstringstream #else typedef ostringstream tstringstream #endif` – Felix Dombek Aug 25 '11 at 03:26
  • 3
    This has been asked so many times before: http://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c http://stackoverflow.com/questions/273908/c-integer-stdstring-conversion-simple-function http://stackoverflow.com/questions/5555616/converting-integer-to-string-c http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c http://stackoverflow.com/questions/4668760/converting-an-int-to-stdstring just to name a few – Adam Rosenfield Aug 25 '11 at 03:26
  • The "duplicate" posts cited do not seem to be converting an int to LPCTSTR but rather a std::string. – Nick Aug 25 '11 at 03:45
  • 1
    @Nick: `LPCTSTR` is a typedef to `const char*` or `const wchar_t*` depending on whether `UNICODE` or `_UNICODE` has been defined. You can get a `const char*` from a `std::string` or a `const wchar_t*` from a `std::wstring` via the `c_str()` method of `string`/`wstring`. – In silico Aug 25 '11 at 04:03
  • @Jack: The answer in the "Formatting an integer in C++" question shows a `ostringstream` being used, which is non-Unicode. I simply noted that `wostringstream` can be used if Unicode is needed. Felix Dombek provided a way to automatically select which one to use. – In silico Aug 25 '11 at 04:08
  • @in silico I see. I am not explicitly defining UNICODE. I appreciate the clarification. – Nick Aug 25 '11 at 04:13
  • @Nick With Visual Studio, UNICODE is defined automatically by default for new projects. This is configured through the project settings (though I *strongly* suggest using UNICODE for projects targetting Win32). – Sven Aug 25 '11 at 04:30
  • the lesson here is to stop casting when the compiler tells you that you made a mistake. – David Heffernan Aug 25 '11 at 08:26

5 Answers5

6

Converting for MFC like belov :

int number = 1;

CString t;

t.Format(_T("%d"), number);

AfxMessageBox(t);

I used and it worked for me.

Mahmut EFE
  • 5,137
  • 5
  • 46
  • 56
  • Note that `AfxMessageBox` receives `LPCTSTR`, not `CString`. The above code works because CString (CSimpleStringT) has `operator PCXSTR()`. – starriet Jul 02 '23 at 01:26
3
int OurVariable;

LPCWSTR result=(to_string(OurVariable).c_str());

or

LPCWSTR result=LPCSTR(to_string(OurVariable).c_str());

or

LPCSTR result=(to_string(OurVariable).c_str());

it really works

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
3

nA few ways:

int value = 42;
TCHAR buf[32];
_itot(value, buf, 10);

another more friendly way for your case:

int value = 42;
const size_t buflen = 100;
TCHAR buf[buflen];
_sntprintf(buf, buflen - 1, _T("the value is %d"), value);
Jack
  • 1,477
  • 15
  • 20
  • So _stprintf will put the formatted string the TCHAR buffer which can then be cast to LPCTSTR? – Nick Aug 25 '11 at 03:19
  • @Nick, `LPCTSTR` is just another name for `const TCHAR *`. When you use an array of `TCHAR` in a context that wants `TCHAR *` or `const TCHAR *` the compiler will automatically provide a pointer for you. – Mark Ransom Aug 25 '11 at 04:13
  • 1
    @Jack, you need to wrap that string literal in the `_sntprintf` call with a `_T()`. – Mark Ransom Aug 25 '11 at 04:14
  • Since he's using `TCHAR` instead of `_TCHAR`, shouldn't the string literal be wrapped with `TEXT()` instead of `_T()`? –  Aug 25 '11 at 17:02
3

LPCTSTR is defined like this:

#ifdef  UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef const char* LPCTSTR;
#endif

std::string::c_str() returns a const char* only. You can't convert a const char* directly to const wchar_t*. Normally the compiler will complain about it, but with the LPCTSTR cast you end up forcing the compiler to shut up about it. So of course it doesn't work as you expect at runtime. To build on what you have in your question, what you probably want is something like this:

// See Felix Dombek's comment under OP's question.
#ifdef UNICODE
typedef std::wostringstream tstringstream;
#else
typedef std::ostringstream tstringstream;
#endif

int volumeLevel = 6;    
tstringstream stros;    
stros << volumeLevel;     
::MessageBox(plugin.hwndParent, stros.str().c_str(), L"", MB_OK);  
In silico
  • 51,091
  • 10
  • 150
  • 143
  • 1
    If you don't want to use the typedef, you could also use `std::basic_ostringstream`. – Sven Aug 25 '11 at 04:27
  • 1
    This in conjunction with a good article on MS strings is good start :) Sorry for my ignorance with w_char_t vs char. I need to do some research. Thanks for all the help. – Nick Aug 25 '11 at 04:40
-1

Use _T() decorator for Unicode-aware code:

int number = 1;

CString t;

t.Format(_T("%d"), number);

AfxMessageBox(t);

ref: https://social.msdn.microsoft.com/Forums/vstudio/en-US/f202b3df-5849-4d59-b0d9-a4fa69046223/how-to-convert-int-to-lpctstr?forum=vclanguage

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
user9559196
  • 157
  • 6
  • 1
    Your answer might be clearer if you used the same variable names as the original poster's question. Thanks. – rwp Apr 25 '18 at 16:05
  • Duplicated answer. Already a user answered. You just copied and pasted it here. Redundant work. – Mahmut EFE Jul 07 '23 at 00:22