Click to See Complete Forum and Search --> : WINAPI converting int to CHAR to display as a Static's text


Icyculyr
March 5th, 2008, 08:22 PM
I want to convert an int to a CHAR or string or store it in a buffer, or whatever the best way to convert int to char etc..

I have created a static called hStatic, and need to do this:

CHAR cBuffer; //Store this value "iPlayerX = (iPlayerX), iMapX = (iMapX)"
//The problem is how do I store those values into cBuffer, both string and int.
SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)cBuffer);


Does anyone know how to do this? I am creating a windows application (a 2D Game with graphics)

Cheers

kabilius
March 5th, 2008, 09:38 PM
CHAR cBuffer; //Store this value "iPlayerX = (iPlayerX), iMapX = (iMapX)"
//The problem is how do I store those values into cBuffer, both string and int.
SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)cBuffer);
Cheers


Hi Icyculyr,

I can't tell what the type of iPlayerX or iMapX is from your statements, but here is one way on how to convert int to char of arrays.


int nValue = 20 ;
char szChar[20] = "" ;
sprintf ( szChar, "%d", nValue ) ;


hope this helps,
kab

Icyculyr
March 5th, 2008, 09:44 PM
iPlayerX & iMapX are ints,

I don't have the function sprintf()?

What do I include?

kabilius
March 6th, 2008, 01:34 AM
Hi Icyculyr,

#include <stdio.h> is what you need.
Try to use google, it has a lot of knowledge you can acquire.

kab

Icyculyr
March 6th, 2008, 02:29 AM
I only have access to the internet at certain periods of the day, and I don't get long to spend, so I just use the MSDN documentation (comes with Visual Studio), and google takes a while (for me)

Anyway thanks, I'll try that.

Cheers

Boris K K
March 6th, 2008, 05:31 AM
Or just use SetDlgItemInt. It will work even for non-dialog parent window as long as hStatic has a unique ID.

Alternatively, for "purists" that like to use WinAPI whenever possible, there is wsprintf ;)

Icyculyr
March 8th, 2008, 09:38 PM
Edit: I thought I would put the code anyway:

CHAR szChar[20] = "";
sprintf(szChar, "%d", 20);

SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)szChar);

that just shows the character '|'

I tried using sprintf, and it didn't work, and I didn't understand how to use wsprintf, or SetDlgItemInt, (didn't know what to do for the ID)..

I'd appreciate it if someone could zip up a small sln in Visual Studio (if anyone uses it) of how to do this.. because I can't get it working...

The value in szChar after using sprintf, is all odd characters (looks like binary or some strange text format) it's mostly lines like |||||||..

Cheers

Boris K K
March 10th, 2008, 07:59 AM
1. There is simply no way szChar to contain strange characters immediately after the call to sprintf. Your code is OK as long as you compile without UNICODE defined (which means it is not that OK).

2. Use wsprintf just like sprintf, except that it does not support floating-point numbers.

3. For SetDlgItemInt you need to defne a unique child window ID in hMenu parameter (type cast required) of CreateWindow/CreateWindowEx. Or, if the window is created from a dialog template, just use the resource editor to change the ID.

wey97
March 12th, 2008, 10:47 AM
I would use a standards compliant method.


#include <sstream>
using std::ostringstream;
using std::ends;


ostringstream oss;
oss << 20 << ends;

SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)oss.str().c_str());

asalways
March 12th, 2008, 10:54 AM
I use VS2005 and it always reports warnings as well as errors if I don't use TCHAR

wey97
March 12th, 2008, 11:06 AM
I use VS2005 and it always reports warnings as well as errors if I don't use TCHAR

That's because the default in VS2005 is UNICODE.

Igor Vartanov
March 12th, 2008, 12:23 PM
TCHAR szChar[20] = {0};
wsprintf(szChar, TEXT("%d"), 20);

SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)szChar);

Icyculyr
March 12th, 2008, 07:49 PM
Thanks, and to convert a string such as "hello" would be

TCHAR szChar[20] = {0};
wsprintf(szChar, TEXT("%s"), "hello");

SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)szChar);

Igor Vartanov
March 13th, 2008, 06:59 AM
Nope, it would be enough to do this:
SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)TEXT("hello"));
just because "hello" is already a text string. :)