user198725878
user198725878

Reputation: 6386

QString::fromWCharArray gives strange charracters

I have a string named aDrive = "H:/"

i want to convert this string into WCHAR so used like below

WCHAR Drive[4];

aDrive.toWCharArray ( Drive ) ;

when i printed it qDebug ()<<QString::fromWCharArray ( Drive );

it displays like "H:/???"

why i get the starnge charracters at the end..

Thank you for your time

Upvotes: 0

Views: 2396

Answers (2)

jfs
jfs

Reputation: 16798

This is just my guess.

According to the toWCharArray documentation: This function does not append a null character to the array. The returned string was not properly terminated with null. When you printed it, the part of memory after the memory allocated for the Drive array was also printed until a null byte is reached.

Upvotes: 1

laalto
laalto

Reputation: 152927

QString::toWCharArray() does not zero-terminate the array. Without an explicit array length with QString::fromWCharArray(), it will read wchars until a zero wchar is read. In this case, you'll have to add the zero wchar yourself at the end, or use explicit length parameter with QString::fromWCharArray().

As always, the documentation is your friend.

Upvotes: 4

Related Questions