Sign in to follow this  
xhantos

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

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 this post


Link to post
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 this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
I guess what you really wanted was:


out << number1 + number2;

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this