CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    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

  2. #2
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: WINAPI converting int to CHAR to display as a Static's text

    Quote 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

  3. #3
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    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?

  4. #4
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: WINAPI converting int to CHAR to display as a Static's text

    Hi Icyculyr,

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

    kab

  5. #5
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    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

  6. #6
    Join Date
    Aug 2004
    Posts
    294

    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/

  7. #7
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    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.

  8. #8
    Join Date
    Aug 2004
    Posts
    294

    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/

  9. #9
    Join Date
    Dec 2002
    Location
    Birmingham, AL
    Posts
    82

    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());

  10. #10
    Join Date
    Aug 2007
    Posts
    22

    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

  11. #11
    Join Date
    Dec 2002
    Location
    Birmingham, AL
    Posts
    82

    Re: WINAPI converting int to CHAR to display as a Static's text

    Quote 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.

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  13. #13
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    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);

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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
  •  





Click Here to Expand Forum to Full Width

Featured