| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 1 | |
Hi, How to convert a QString to a char array? For example: char array[255]; QString str; -- [ signature omitted ]
Hi, Both don't work. The two methods both convert QString to char *, not an array of char. 2007/6/2, 张凤丽 <zhangfenglisdu@xxxxxxxxx>: > > Hi, > How to convert a QString to a char array? For example: > char array[255]; > QString str; > > > -- > Best Regards > Fengli Zhang -- [ signature omitted ]
On 02.06.07 21:52:03, ååä wrote: > Hi, > Both don't work. > The two methods both convert QString to char *, not an array of char. If you have char a[128] a == char*, so doing something like QByteArray ba = somestring.toLocal8Bit(); a=ba.data(); works fine (Note: a gets invalid as soon as ba is destroyed), except that your array might be larger than 128 after that. If you just want to copy the first 128 characters something like strncpy(ba.data(),a,128); should work (untested). Andreas -- [ signature omitted ]
On Saturday 02 June 2007, Andreas Pakulat wrote: > If you have > > char a[128] > > a == char*, so doing something like ??? > QByteArray ba = somestring.toLocal8Bit(); > a=ba.data(); compiler error: a is const, since it is an array and not a pointer. Remember: arrays and pointers are equivalent, not identical. > works fine (Note: a gets invalid as soon as ba is destroyed), except > that your array might be larger than 128 after that. If you just want > to copy the first 128 characters something like > > strncpy(ba.data(),a,128); > > should work (untested). This should read: //copy max. 128 bytes strncpy(a,ba.data(),128); //make sure the string is terminated a[127]=0; Konrad
Attachment:
Attachment:
pgpWVrrqblyRC.pgp
Description: PGP signature
Message 5 in thread
On Saturday 02 June 2007 14:31:16 ååä wrote:
> Hi,
> How to convert a QString to a char array? For example:
> char array[255];
> QString str;
Fixed char arrays are generally a bad idea. But if you really want to do this:
char array[255];
QString qstr = getMyQString();
if (qstr.size()>size(array)-1) {
throw runtime_exception("Failed to convert qstr->array since qstr didn't
fit");
}
strcpy(array, qstr.data());
If you just need a c-string, I'd suggest using data() directly or use
strdup():
char* mycstring = strdup(qstr.data());
remember to have a mechanism to delete the mycstring pointer.
Hope this helps.
--
[ signature omitted ]
Message 6 in thread
Hi,
Haveing fix the problem by refering all your suggestion. Doing it this way:
char array[255];
QString str;
strcpy(array, str.toAscii().constData());
2007/6/2, 张凤丽 <zhangfenglisdu@xxxxxxxxx>:
>
> Hi,
> How to convert a QString to a char array? For example:
> char array[255];
> QString str;
>
>
> --
> Best Regards
> Fengli Zhang
--
[ signature omitted ]
Message 7 in thread
On Sunday 03 June 2007 04:00:23 ååä wrote:
> Hi,
> Haveing fix the problem by refering all your suggestion. Doing it this way:
> char array[255];
> QString str;
> strcpy(array, str.toAscii().constData());
Unless you know that str.size()<255, the above is a stack overflow, with all
that entails. If I were you, I'd add a check on the size even so, just
because comparing 2 size_t are so extremely cheap. Just
if (str.size() < sizeof(array)-1) terminate()
would be an substantial improvement.
You should also be aware that if str could contain '0x00' chars, the string in
the array would be truncated.
--
[ signature omitted ]
Message 8 in thread
I think str.data() will do the job…
________________________________
From: 张凤丽 [mailto:zhangfenglisdu@xxxxxxxxx]
Sent: Saturday, June 02, 2007 6:01 PM
To: qt-interest
Subject: convert QString to a char array
Hi,
How to convert a QString to a char array? For example:
char array[255];
QString str;
--
[ signature omitted ]