locked
Converting a file path from Platform::String to MultiByte character set for 3rd party libs

    Question

  • A 3rd party libs expects a filepath as char* input and my WinRT component gets the apps localfolder using the WinRT API. However this is a UNICODE path (that can contain even japanese or cyrillic chars).

    What is the recommended way of converting such path to ANSI char*? Should the WideCharToMultiByte function be used for that and if so, what are the correct flags and code page?

    Wednesday, June 06, 2012 10:41 AM

Answers

  • WideCharToMultiByte is supported in the metro style application. You can also use CRT function wcstombs_s to do the change. It's more esay to use. Here is my code for your reference:

    const wchar_t* orig=strInput->Data();
    size_t origsize = wcslen(orig) + 1;
    size_t convertedChars = 0;
    char dst[256];
    wcstombs_s(&convertedChars, dst, origsize, orig, _TRUNCATE);

    Best Regards,
    Han Xia

    • Marked as answer by Jesse Jiang Wednesday, June 20, 2012 9:01 AM
    Thursday, June 07, 2012 2:41 AM
    Moderator
  • Yes, you can use WideCharToMultiByte. The correct flags and code page will depend on what you are trying to do: you will want to use a code page the target library accepts and which can represent your input value. Not all characters can be represented in every code page, so you will need to make sure that you handle cases where your conversion can fail.

    Here's a quick code snippet which assumes your library accepts UTF-8:

    	// Set up a string to convert

    String^ str = ref new String(L"Lorem ipsum dolor sit amet\r\n");

    // Allocate enough space for the new string int cb = WideCharToMultiByte(CP_UTF8,0,str->Data(),-1,nullptr,0,nullptr,nullptr); char* sz = new char[cb]; // Convert it to UTF-8 if (WideCharToMultiByte(CP_UTF8,0,str->Data(),-1,sz,cb,nullptr,nullptr) > 0) { OutputDebugStringA(sz); }

    --Rob

    • Marked as answer by Jesse Jiang Wednesday, June 20, 2012 9:01 AM
    Thursday, June 07, 2012 2:41 AM
    Owner

All replies

  • WideCharToMultiByte is supported in the metro style application. You can also use CRT function wcstombs_s to do the change. It's more esay to use. Here is my code for your reference:

    const wchar_t* orig=strInput->Data();
    size_t origsize = wcslen(orig) + 1;
    size_t convertedChars = 0;
    char dst[256];
    wcstombs_s(&convertedChars, dst, origsize, orig, _TRUNCATE);

    Best Regards,
    Han Xia

    • Marked as answer by Jesse Jiang Wednesday, June 20, 2012 9:01 AM
    Thursday, June 07, 2012 2:41 AM
    Moderator
  • Yes, you can use WideCharToMultiByte. The correct flags and code page will depend on what you are trying to do: you will want to use a code page the target library accepts and which can represent your input value. Not all characters can be represented in every code page, so you will need to make sure that you handle cases where your conversion can fail.

    Here's a quick code snippet which assumes your library accepts UTF-8:

    	// Set up a string to convert

    String^ str = ref new String(L"Lorem ipsum dolor sit amet\r\n");

    // Allocate enough space for the new string int cb = WideCharToMultiByte(CP_UTF8,0,str->Data(),-1,nullptr,0,nullptr,nullptr); char* sz = new char[cb]; // Convert it to UTF-8 if (WideCharToMultiByte(CP_UTF8,0,str->Data(),-1,sz,cb,nullptr,nullptr) > 0) { OutputDebugStringA(sz); }

    --Rob

    • Marked as answer by Jesse Jiang Wednesday, June 20, 2012 9:01 AM
    Thursday, June 07, 2012 2:41 AM
    Owner