Page 1 of 1

Problem with string conversion to UaString type when using Japanese characters

Posted: 04 Oct 2022, 15:46
by IgnacioB
Hello all,

Thank you for let me write in this forum.

I am running a piece of code like this:

//"input_BSTR" parameter is a BSTR type variable
auto var_bstrt = _bstr_t(input_BSTR);
auto var_wchart = static_cast<wchar_t*>(var_bstrt);
auto var_wchart_2 = var_wchart;

//This next line of code is the one giving me problems
UaString result_UaString = var_wchart_2;

I am passing into "input_BSTR" a string with Japanese characters. When I read the "result_UaString" the Japanese characters on it have been modified.

So, my questions are:

- Is there a way to convert a BSTR (or _bstr_t) string to a UaString string without changing the Japanese characters? So, in my code, this would be convert "input_BSTR" (or "var_bstrt") to "result_UaString"

- If the previous point is not possible, would it be possible to convert from a std::wstring to a UaString (without changing the Japanese characters inside)?

Thank you very much for your help. It is really appreciated.
Best regards,

Ignacio

Re: Problem with string conversion to UaString type when using Japanese characters

Posted: 13 Oct 2022, 09:18
by Support Team
Hello Ignacio,

you can use the constructor of the UaString class taking an UTF-16 encoded string.
see: https://documentation.unified-automation.com/uasdkcpp/1.7.7/html/classUaString.html#a60619d72a3dab1c4ee0e75442742c124

example:

Code: Select all

    // create a japanese string "オートメーション" - BSTR is UTF-16 encoded
    BSTR myBstr = SysAllocString(L"\x30aa\x30fc\x30c8\x30e1\x30fc\x30b7\x30e7\x30f3");
    UaString uaString(myBstr);
Let's have a look at the first character "オ" with the unicode number U+30AA
The UTF-16 encoded data in memory is hex 30 AA - used in BSTR.
The UTF-8 encoded data in memory is hex E3 82 AA - used in UaString.
see also https://unicode-table.com/en/30AA/

If you can't see the "オートメーション" string in you IDE / debugger that is because your IDE does not know how decode the UaString for the watch window. However if you look at the content you should see the UTF-8 encoded data.

Re: Problem with string conversion to UaString type when using Japanese characters

Posted: 18 Oct 2022, 14:54
by IgnacioB
Thank you really much for your help, I'll appreciate it. Now we have more knowledge about how to do it thanks to your answer. Best regards,

Ignacio