none
Convert char arr[100]="Howdy" -- to -- (LPCWSTR)L"Howdy"

    Question

  • OK, I've attempted to grind through the zillion online explanations on converting 8 bit characters to 16 bit characters...

    But it seems I've found ever example and explanation except the right one... So maybe some kind soul would just illustrate with a simple code sample how to do this...

    This works:

    MessageBox(NULL, (LPCWSTR)L"Message Goes Here", (LPCWSTR)L"Message Box Title Goes Here",MB_OK);

    but if the 'message' is already in an existing char array... that you can not destroy during any conversion...

    char myArray[100] = "Howdy";

    So how would you code the MessageBox to display the characters in myArray?

    Thanks for any help.

    Wednesday, June 06, 2012 7:45 PM

Answers

  • On 6/6/2012 3:45 PM, Mel_3 wrote:

    OK, I've attempted to grind through the zillion online explanations on converting 8 bit characters to 16 bit characters...

    Did any of them mention MultiByteToWideChar?

    This works:

    MessageBox(NULL, (LPCWSTR)L"Message Goes Here", (LPCWSTR)L"Message Box Title Goes Here",MB_OK);

    Drop the casts, you don't need them. They are harmless in this case, but they don't do anything.

    char myArray[100] = "Howdy";

    So how would you code the MessageBox to display the characters in myArray?

    If you are limited to pure WinAPI with no helper libraries, then something like this:

    int size = MultiByteToWideChar(CP_ACP, 0, myArray, -1, NULL, 0);
    assert(size > 0);    // error handling left as an exercise for the reader
    WCHAR* message = new WCHAR[size];
    size = MultiByteToWideChar(CP_ACP, 0, myArray, -1, message, size);
    assert(size > 0);
    MessageBox(0, message, L"Title", MB_OK);
    delete[] message;

    Igor Tandetnik

    Wednesday, June 06, 2012 8:12 PM
  • Another option is to use MessageBoxA()

    Frankly, you should be asking yourself, "Can I use

    wchar_t myArray[100] = L"Howdy";

    instead?"


    Wednesday, June 06, 2012 8:49 PM
  • On 6/6/2012 4:53 PM, Wyck wrote:

        char myArray[100] = "Howdy";
        std::wstringstream ss;
        ss<<  myArray;

    Does this actually work? I'm too lazy to test, but I'm pretty sure wstringstream doesn't have operator<< overload taking char*, so this would use operator<<(void*) and print the address of myArray, not its contents.


    Igor Tandetnik

    I was worried about that too, but it works. I tried it.  See the ostream header for the template of operator << that takes a const char *.

     template<class _Elem,
    	class _Traits> inline
    	basic_ostream<_Elem, _Traits>& __CLRCALL_OR_CDECL operator<<(
    		basic_ostream<_Elem, _Traits>& _Ostr, const char *_Val)
    	{	// insert NTBS
    

    In debug mode you can watch it go character by character calling the widen function which ultimately lands in _Mbrtowc.  (xmbtowc.c)

    Thursday, June 07, 2012 5:01 AM

All replies

  • On 6/6/2012 3:45 PM, Mel_3 wrote:

    OK, I've attempted to grind through the zillion online explanations on converting 8 bit characters to 16 bit characters...

    Did any of them mention MultiByteToWideChar?

    This works:

    MessageBox(NULL, (LPCWSTR)L"Message Goes Here", (LPCWSTR)L"Message Box Title Goes Here",MB_OK);

    Drop the casts, you don't need them. They are harmless in this case, but they don't do anything.

    char myArray[100] = "Howdy";

    So how would you code the MessageBox to display the characters in myArray?

    If you are limited to pure WinAPI with no helper libraries, then something like this:

    int size = MultiByteToWideChar(CP_ACP, 0, myArray, -1, NULL, 0);
    assert(size > 0);    // error handling left as an exercise for the reader
    WCHAR* message = new WCHAR[size];
    size = MultiByteToWideChar(CP_ACP, 0, myArray, -1, message, size);
    assert(size > 0);
    MessageBox(0, message, L"Title", MB_OK);
    delete[] message;

    Igor Tandetnik

    Wednesday, June 06, 2012 8:12 PM
  • Another option is to use MessageBoxA()

    Frankly, you should be asking yourself, "Can I use

    wchar_t myArray[100] = L"Howdy";

    instead?"


    Wednesday, June 06, 2012 8:49 PM
  • Alternate approach:

    #include <windows.h>
    #include <sstream>
    
    void Test( const wchar_t* str )
    {
    	OutputDebugStringW( str );
    }
    
    int main()
    {
    	char myArray[100] = "Howdy";
    	std::wstringstream ss;
    	ss << myArray;
    	Test( ss.str().c_str() );
    
    	return 0;
    }

    Wednesday, June 06, 2012 8:53 PM
  • OK Guys... Everyone's suggestion worked... code below for others wishing to learn.

    Note that I built this as a Win32 Console App as I couldn't get it to work as a Win32 App...
    .. can I correct code to get it to compile as Win32 App?
    .. or what about a Windows Forms Application?

    (BTW, in my original case I was using it in a DLL.)

    Here's the code with a few comments. Let me know of any bugs but it seems to compile and run. Thanks everyone!

    #include<stdio.h>
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    /* used by demo 1 start */
    #include<Windows.h>
    #include<assert.h>
    #include<wchar.h>
    /* used by demo 1 end */
    
    /* used with demo3 - start */
    #include <sstream>
    void Test( const wchar_t* str )
    {
    	OutputDebugStringW( str );	//outputs to VC++ IDE Output window
    }
    /* used with demo3 - end */
    
    int main()
    {
    	char myArray[100]="Howdy";
    
    	/* demo 1 */
    	int size = MultiByteToWideChar(CP_ACP, 0, myArray, -1, NULL, 0);
    	//assert(size > 0);    // error handling left as an exercise for the reader - I read this is for debug only
    	WCHAR* message = new WCHAR[size];
    	size = MultiByteToWideChar(CP_ACP, 0, myArray, -1, message, size);
    	//assert(size > 0);
    	MessageBox(0, message, L"Demo1 - Thanks Igor!", MB_OK);
    	delete[] message;
    
    	/* demo 2 */
    	MessageBoxA(0, "It Works!", "Demo2 - Thanks Brian!", MB_OK);	// 'A' must be for ascii &/or 8-bit characters
    	MessageBoxA(0,myArray,"Demo2a - Thanks Brian!",MB_OK);			// and the array works w/o any special treatment
    
    	/* demo 3 */									//use this one for debugging only as text appers only in IDE Output window
    	char myArray2[100]="Demo3 - Thanks Wyck!";
    	std::wstringstream ss;
    	ss << myArray2;
    	Test( ss.str().c_str() );
    
    	return 0;
    }


    • Edited by Mel_3 Wednesday, June 06, 2012 9:51 PM
    Wednesday, June 06, 2012 9:48 PM
  • On 6/6/2012 4:53 PM, Wyck wrote:

        char myArray[100] = "Howdy";
        std::wstringstream ss;
        ss<<  myArray;

    Does this actually work? I'm too lazy to test, but I'm pretty sure wstringstream doesn't have operator<< overload taking char*, so this would use operator<<(void*) and print the address of myArray, not its contents.


    Igor Tandetnik

    Wednesday, June 06, 2012 10:39 PM
  • Please consider using "Hello, world" instead of "Howdy" if you really
    want to be a real programmer
     
    Thursday, June 07, 2012 12:23 AM
  • On 6/6/2012 4:53 PM, Wyck wrote:

        char myArray[100] = "Howdy";
        std::wstringstream ss;
        ss<<  myArray;

    Does this actually work? I'm too lazy to test, but I'm pretty sure wstringstream doesn't have operator<< overload taking char*, so this would use operator<<(void*) and print the address of myArray, not its contents.


    Igor Tandetnik

    I was worried about that too, but it works. I tried it.  See the ostream header for the template of operator << that takes a const char *.

     template<class _Elem,
    	class _Traits> inline
    	basic_ostream<_Elem, _Traits>& __CLRCALL_OR_CDECL operator<<(
    		basic_ostream<_Elem, _Traits>& _Ostr, const char *_Val)
    	{	// insert NTBS
    

    In debug mode you can watch it go character by character calling the widen function which ultimately lands in _Mbrtowc.  (xmbtowc.c)

    Thursday, June 07, 2012 5:01 AM
  • ...

    #include <atlcom.h>

    ...

    USE_CONVERSIONS;   // May be USES_CONVERSION;   I don't remember clearly.

    LPCWSTR pUStr=A2W(pAStr);

    Thursday, June 07, 2012 10:37 AM
  • On 07/06/2012 12:37, yeshirow_ wrote:

    ...

    #include<atlcom.h>

    ...

    USE_CONVERSIONS;   // May be USES_CONVERSION;   I don't remember clearly.

    LPCWSTR pUStr=A2W(pAStr);

    This is an obsolete ATL3/VC6 form.

    The more modern version doesn't need USES_CONVERSION, and just use robust RAII helpers like CA2W:

    http://msdn.microsoft.com/en-us/library/87zae4a3(v=vs.80).aspx

    char msg[...];
    char title[...];
    
    MessageBox(NULL, CA2W(msg), CA2W(title), MB_OK);
    

    Giovanni

    Thursday, June 07, 2012 10:56 AM