Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, January 2008
QString from *buff and len


Message 1 in thread

Hi!

Is there an easy way to create a QString from a "char *buff" (not NULL 
terminated) and an integer which holds the length of the buffer without 
doing a conversion to null-terminated string first?

thanks
Klaus

--
 [ signature omitted ] 

Message 2 in thread

> Is there an easy way to create a QString from a "char *buff" 
> (not NULL 
> terminated) and an integer which holds the length of the 
> buffer without 
> doing a conversion to null-terminated string first?

try QString(QByteArray(buff,len))

Cheers,
Peter

--
 [ signature omitted ] 

Message 3 in thread

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peter Prade wrote:
>> Is there an easy way to create a QString from a "char *buff" 
>> (not NULL 
>> terminated) and an integer which holds the length of the 
>> buffer without 
>> doing a conversion to null-terminated string first?
> 
> try QString(QByteArray(buff,len))
> 

After spending 30 seconds reading the docs, it appears that way will
result in the string being copied at least twice. It would be better to
use QString(QByteArray::fromRawData(buff,len)), or one of:

QString::fromAscii(buff,len)
QString::fromLatin1(buff,len)
QString::fromLocal8Bit(buff,len)

- --
Jonathan Brady
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHfSB4N8xRV9hCNWcRAjNJAJwOt5JevPv9InuyuwhDXRFxcP8GswCfZRxi
R2HC1kG3ENb5E4XLyknR3Gk=
=W5DX
-----END PGP SIGNATURE-----

--
 [ signature omitted ] 

Message 4 in thread


Jonathan Brady schrieb:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Peter Prade wrote:
>>> Is there an easy way to create a QString from a "char *buff" 
>>> (not NULL 
>>> terminated) and an integer which holds the length of the 
>>> buffer without 
>>> doing a conversion to null-terminated string first?
>> try QString(QByteArray(buff,len))
>>
> 
> After spending 30 seconds reading the docs, it appears that way will
> result in the string being copied at least twice. It would be better to
> use QString(QByteArray::fromRawData(buff,len)), or one of:
> 
> QString::fromAscii(buff,len)
> QString::fromLatin1(buff,len)
> QString::fromLocal8Bit(buff,len)

These look fine. I was not aware of the static members and only took a 
look at the QString constructors.

thanks
Klaus

--
 [ signature omitted ] 

Message 5 in thread

QString     string(QByteArray(buff, length));

Of course, this does its own internal Qt conversions.

Keith
**Please do not reply to me, reply to the list.**

On 01-03-2008 9:15 AM, "Klaus Darilion" wrote:

> Hi!
> 
> Is there an easy way to create a QString from a "char *buff" (not NULL
> terminated) and an integer which holds the length of the buffer without
> doing a conversion to null-terminated string first?
> 
> thanks
> Klaus


--
 [ signature omitted ]