Converting int, dword, byte and so on into TCHAR (C++)

Started by
7 comments, last by taby 19 years, 2 months ago
Most windows functions use the TCHAR, but I have no idea how to handle them, the visual studio help isnt really a "help" on that either. lets say i have this: int number1 = 100; int number2 = 15; And I want a Messagebox to appear and tell me the values of both variables, something like: MessageBox(hwnd,(number1 + number2),"Caption",MB_OK); .. but that of course gives me a wrong type error :(
Advertisement
So it is not ?

Read here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_sprintf.2c_.swprintf.asp
http://www.gamedev.net/columns/books/bookdetails.asp?productid=51

I just have to recommend this book. It is about using win32, and he uses alot of unicode stuff and dealing with many aspects of your question. Very easy to read and understand, lots of examples, and goes through alot of stuff regarding win32 and windows programming.

Quote:Original post by xhantos
Most windows functions use the TCHAR, but I have no idea how to handle them, the visual studio help isnt really a "help" on that either.

lets say i have this:

int number1 = 100;
int number2 = 15;

And I want a Messagebox to appear and tell me the values of both variables, something like:

MessageBox(hwnd,(number1 + number2),"Caption",MB_OK);

.. but that of course gives me a wrong type error :(

int number1 = 100;int number2 = 15;char s[200];sprintf(s, "%d + %d", number1, number2);MessageBox(0, s, "Caption", MB_OK);
Killers don't end up in jailThey end up on a high-score!
ty very much! <3
Beware potential buffer overflows / bloated stacks. sprintf is not necessary if your compiler is fairly C++ compliant:

#include <windows.h>#pragma comment(lib, "user32.lib")#include <sstream>using std::ostringstream;int main(void){	int number1 = 1;	int number2 = 2;	ostringstream out;	out << number1 << " " << number2;	MessageBox(0, out.str().c_str(), "Hi", MB_OK);	return 0;}
I guess what you really wanted was:

out << number1 + number2;
what exactly does the "<<" operator do? C++ sure has alot of weird stuff compared to like java
<< and >> are used to send/get data to/from a stream object like cout, an ostringstream, fstream.

An ostringstream is just very handy for converting numbers to a string representation, as well as building strings that contain mixed types of data.

ie:
int i = 50;float f = 50.0f;ostringstream out;out << "I have " << i << " apples.";out << " However, if I were a mathematician, I would have " << f << " apples.";MessageBox(0, out.str().c_str(), "Hi", MB_OK);


... would produce a message box saying "I have 50 apples. However, if I were a mathematician, I would have 50.000000 apples."

The end result is similar to what you would get with C's sprintf, but with ostringstream you don't have to worry about specifying types (unless you want to), and you don't have to worry about having enough (or too much, wasted) buffer space to hold the final string, since the ostringstream does all of the memory management for you.

the .str() method returns a C++ std::string, which in itself has a .c_str() method, which returns a const char * -- which is, of course, what one uses as the message parameter in MessageBox.

If you think C++ is wierd, take a look at perl. Ha.

This topic is closed to new replies.

Advertisement