[Qt-interest] QString to char array
Avishek_Sharma
Avishek_Sharma at satyam.com
Mon Dec 29 13:29:52 CET 2008
Hi,
Thanks for the suggestions.They have been a great help.
Avi
________________________________________
From: qt-interest-bounces at trolltech.com [qt-interest-bounces at trolltech.com] On Behalf Of Jonathan Kew [jonathan_kew at sil.org]
Sent: Monday, December 29, 2008 4:13 PM
To: qtInterest Interest
Subject: Re: [Qt-interest] QString to char array
On 29 Dec 2008, at 06:43, Robin Helgelin wrote:
> On Mon, Dec 29, 2008 at 05:31, Avishek_Sharma <Avishek_Sharma at satyam.com
> > wrote:
>> Hi,
>> I came across this piece of code to convert/copy from QString to
>> char array.
>>
>> char arr[255];
>> QString str;
>> strcpy(arr,str.toAscii().constData());
>>
>> will this code work?
>
> Yes, it works, but your example is a dangerous when you are copying to
> a fixed sized array, better use strncpy in such cases.
You'll also have trouble if the QString contains non-Western
characters. See under QString::toLatin1(), which tells us "The
returned byte array is undefined if the string contains non-Latin1
characters." Users don't usually appreciate apps that behave in
"undefined" ways!
You should probably be using UTF-8 if you really need to put this into
an array of 8-bit chars. But first you may need to read up about
Unicode and other character encodings, and make sure you know what
encodings are expected by whatever APIs you're using.
JK
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.
More information about the Qt-interest-old
mailing list