-
March 5th, 2008, 08:22 PM
#1
WINAPI converting int to CHAR to display as a Static's text
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
-
March 5th, 2008, 09:38 PM
#2
Re: WINAPI converting int to CHAR to display as a Static's text
Originally Posted by Icyculyr
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.
Code:
int nValue = 20 ;
char szChar[20] = "" ;
sprintf ( szChar, "%d", nValue ) ;
hope this helps,
kab
-
March 5th, 2008, 09:44 PM
#3
Re: WINAPI converting int to CHAR to display as a Static's text
iPlayerX & iMapX are ints,
I don't have the function sprintf()?
What do I include?
-
March 6th, 2008, 01:34 AM
#4
Re: WINAPI converting int to CHAR to display as a Static's text
Hi Icyculyr,
is what you need.
Try to use google, it has a lot of knowledge you can acquire.
kab
-
March 6th, 2008, 02:29 AM
#5
Re: WINAPI converting int to CHAR to display as a Static's text
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
-
March 6th, 2008, 05:31 AM
#6
Re: WINAPI converting int to CHAR to display as a Static's text
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
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com/
-
March 8th, 2008, 09:38 PM
#7
Re: WINAPI converting int to CHAR to display as a Static's text
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
Last edited by Icyculyr; March 8th, 2008 at 09:47 PM.
-
March 10th, 2008, 06:59 AM
#8
Re: WINAPI converting int to CHAR to display as a Static's text
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.
Last edited by Boris K K; March 10th, 2008 at 07:12 AM.
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com/
-
March 12th, 2008, 09:47 AM
#9
Re: WINAPI converting int to CHAR to display as a Static's text
I would use a standards compliant method.
Code:
#include <sstream>
using std::ostringstream;
using std::ends;
ostringstream oss;
oss << 20 << ends;
SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)oss.str().c_str());
-
March 12th, 2008, 09:54 AM
#10
Re: WINAPI converting int to CHAR to display as a Static's text
I use VS2005 and it always reports warnings as well as errors if I don't use TCHAR
-
March 12th, 2008, 10:06 AM
#11
Re: WINAPI converting int to CHAR to display as a Static's text
Originally Posted by asalways
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.
-
March 12th, 2008, 11:23 AM
#12
Re: WINAPI converting int to CHAR to display as a Static's text
Code:
TCHAR szChar[20] = {0};
wsprintf(szChar, TEXT("%d"), 20);
SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)szChar);
Best regards,
Igor
-
March 12th, 2008, 06:49 PM
#13
Re: WINAPI converting int to CHAR to display as a Static's text
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);
-
March 13th, 2008, 05:59 AM
#14
Re: WINAPI converting int to CHAR to display as a Static's text
Nope, it would be enough to do this:
Code:
SendMessage(hStatic, WM_SETTEXT, NULL, (LPARAM)TEXT("hello"));
just because "hello" is already a text string.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|