View Full Version : SDK int to tchar.


ClassicThunder
Jun 28, 2007, 12:45 PM
How do you in the SDK convert a into to a tchar?

Thanks,
ClassicThunder

Yakk
Jun 28, 2007, 02:38 PM
It depends on what kind of conversion you want to do.

Do you have an int, and want a wchar_t string?

Do you have an int, and want to store it's bits in a wchar_t?

The question you ask is analagous to "I have some flour, how do I convert it into a dessert?"

ClassicThunder
Jun 28, 2007, 02:51 PM
Opps I meant TCHAR.

I'm using gDLL->logMsg to print messages to a log to help find a crash. And the message is actually a TCHAR. So I have a function that returns an int, and I want that returned int to be printed to a log. How do I turn that int into a TCHAR so the gDLL->logMsg function will log it?

Yakk
Jun 28, 2007, 03:14 PM
You mean TCHAR*?

ClassicThunder
Jun 28, 2007, 03:19 PM
Yes I do mean TCHAR*.

Yakk
Jun 28, 2007, 03:28 PM
template<typename src>
std::wstring ConvertToString( src const& s ) {
std::wstring retval;
std::wstringstream buffer_stream(retval);
buffer_stream << s;
return retval;
}

...

int foo;
...
std::wstring printed_number = ConvertToString( foo );
TCHAR const* c_string = printed_number.c_str();
gDLL->logMsg( ... c_string ... );


Note that none of the above "..." should be typed -- ... just means "you put code here".

foo is the name of the variable of type int. ConvertToString will take an int (or half-a-dozen other types) and produce a std::wstring.

A std::wstring is a C++-standard means to hold a buffer of characters, and dispose of it when you are done.

printed_number.c_str() returns a pointer to the internal buffer -- a wchar_t* (or TCHAR*, as I think the Civ4 SDK uses wchar_t's as TCHAR's).

If the above doesn't work, replace wstring with string and wstringstream with stringstream.

Good luck. :)