www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unicode encodings and string literals

reply Lubos Pintes <lubos.pintes gmail.com> writes:
Hi,
I am playing with samples from Petzold's Programming Windows converted 
by Andrej Mitrovic. Many thanks, Andrej. :-)
My question is about string conversion. There is a function in virtually 
every sample named "toUTF16z", which if I understand properly, converts 
string to UTF-16, so that they can be sent to various Windows API 
functions. But string literals, for example in MessageBox, are fine, no 
conversion is needed. I don't understand the magic, what is converted, 
and when?
If some variable was used e.g. "appName.toUTF16z", and not "Error".toUTF16z
Oct 08 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-10-08 10:06, Lubos Pintes wrote:
 Hi,
 I am playing with samples from Petzold's Programming Windows converted
 by Andrej Mitrovic. Many thanks, Andrej. :-)
 My question is about string conversion. There is a function in virtually
 every sample named "toUTF16z", which if I understand properly, converts
 string to UTF-16, so that they can be sent to various Windows API
 functions. But string literals, for example in MessageBox, are fine, no
 conversion is needed. I don't understand the magic, what is converted,
 and when?
 If some variable was used e.g. "appName.toUTF16z", and not "Error".toUTF16z
Without looking at the code, this is my guess: The "toUTF16z" function converts a D string, of any Unicode encoding, to UTF-16 and converts that to a C string. String literals in D have a trailing null character \0 included making them compatible with functions expecting C strings. String variables on the other do not have the trailing null character and therefore needs a conversion. -- /Jacob Carlborg
Oct 08 2012