Public Group

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

This topic is 4882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 :(

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by xhantosMost 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);

ty very much! <3

##### Share on other sites
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;}

##### Share on other sites
I guess what you really wanted was:

out << number1 + number2;

##### Share on other sites
what exactly does the "<<" operator do? C++ sure has alot of weird stuff compared to like java

##### Share on other sites
<< 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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631425
• Total Posts
3000017
×