αλεχολυτ
αλεχολυτ

Reputation: 5039

printing char* string argument into UnicodeString object

Is it possible to print char* argument into UnicodeString object via printf member function?

Following code gives me a wrong result (damaged string):

UnicodeString s;
s.printf(L"%s", "hello");

If I specify L"hello" instead of "hello" it works as expected (strange to me, why it works with "%s" specifier, i think it should be "%ls").

Tested on Embarcadero RAD Studio XE and 10 Seattle by assigning s string to Caption property of the form's Label.

Upvotes: 0

Views: 867

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596437

UnicodeString::printf() is a wrapper for the C-style vsnwprintf() function. In all of C++Builder's C-style printing functions, %s uses a non-standard implementation - it depends on whether the Narrow or Wide version of the function is being called, whereas in the C standard, %s always expects char* instead.

In this case, UnicodeString::printf() calls the Wide vsnwprintf() function, so %s expects a wchar_t* (however, %ls always expects a wchar_t*, per C standards, and %hs always expects a char*, per Borland standards). This way, in String::printf() (and other printing methods), %s is supposed to match the character type of String - char* for AnsiString, wchar_t* for UnicodeString*.

*However, on Android, Embarcadero has not implemented the Wide vsnwprintf(), only the Narrow vsnprintf(), so UnicodeString::printf() (and other printing methods) end up expecting a UTF-8 char* for %s! (which I reported as QC #124607 and RSP-13285).

Upvotes: 2

Related Questions