DWORD to LPCWSTR

    Question

  • How could I convert a dword to lpcwstr

    example:

    DWORD data = 0xffffff;

    MessageBox(NULL, TEXT(data), L"", MB_ICONINFORMATION | MB_OK);

    Can please help me make this code work?


Answers

  • Also consider a solution like this:


    DWORD
    data = 0xFFFFFF;

    TCHAR s[100];

    _stprintf_s(s, _T("%X"), data);

    MessageBox(NULL, s, _T(""), MB_ICONINFORMATION | MB_OK);

    Instead of %X you can use %i or other described in documentation for sprintf. There are more solutions for MFC and STL.

    • Proposed as answer by Monday, May 04, 2009 12:19 PM
    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM
    • Proposed as answer by Belloc Monday, May 04, 2009 12:30 PM
    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM
  • Complementing Jijo's suggestion :

    TCHAR s[5];

    _itot_s((int)data, s, 5, 10);

    will do it.

    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM
    • Edited by Albin Baby Monday, May 04, 2009 12:38 PM
    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM

All replies

  • MessageBox(NULL, TEXT((LPCWSTR)data), L"", MB_ICONINFORMATION | MB_OK);

  • Error    1    error C2275: 'LPCWSTR' : illegal use of this type as an expression

    I encountered this error...


  • Is there a way to convert dword to string directly?
    Like 0xffffff to "16777215"

  • The 2nd argument to MessageBox() has to be a string of characters (UNICODE, or ASCii)

    (LPCWSTR)data will not add the null character at the end of 0xffffff

  • Also consider a solution like this:


    DWORD
    data = 0xFFFFFF;

    TCHAR s[100];

    _stprintf_s(s, _T("%X"), data);

    MessageBox(NULL, s, _T(""), MB_ICONINFORMATION | MB_OK);

    Instead of %X you can use %i or other described in documentation for sprintf. There are more solutions for MFC and STL.

    • Proposed as answer by Albin Baby Monday, May 04, 2009 12:19 PM
    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM
    • Proposed as answer by Belloc Monday, May 04, 2009 12:30 PM
    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM
  • Error    1    error C2664: 'sprintf' : cannot convert parameter 1 from 'TCHAR [255]' to 'char *'

    I encountered this error

    It also says that it is depreciated

    String convertions i'm using sprintf_s

    I forgot to say that just a newbie in C++ Win32 sorry for that.

  • Use _stprintf_s() instead of sprintf()

  • Complementing Jijo's suggestion :

    TCHAR s[5];

    _itot_s((int)data, s, 5, 10);

    will do it.

    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM
    • Edited by Albin Baby Monday, May 04, 2009 12:38 PM
    • Marked as answer by Wesley Yao Monday, May 11, 2009 2:14 AM