PDA

View Full Version : Problem in converting QString to QChar array?



KaKa
19th March 2007, 00:11
Before I used something like this:

Say theString is a QString w.

And I had this line of code:

for(i=0; theString[i]!=QChar('\0'); i++)
{
if(theString[i]==QChar('('))
{
... ...
}
}

I ran the debug and found that altough i keeps on increasing from 0 to 1 to 2 and so on,

The array under "theString" has only one entry "[0]" whose value is "46".

I don't know how that happened, can someone help me?

Also, I remember checked somewhere saying that if you transform a C styled string into

QString, the QString won't be terminated by '\0'. I am concerning if my case is under this condition:

My "theString" has gone through several transformations:

First it is a QString, then it is strcpy ed to a char array, then this char array was passed as a parameter of function a, and the parameter requires a char*. After that, it is transferred from function a to function b, still as a parameter, but this time the parameter requires a QString. And then it is in function b that I want to convert this QString to QChar array.

Without converting to QChar array, this QString seemed to work fine.

Thanks

jacek
19th March 2007, 00:23
for(i=0; theString[i]!=QChar('\0'); i++)
Better use:
for( int i = 0; i < theString.size(); ++i) {
...
}


First it is a QString, then it is strcpy ed to a char array, then this char array was passed as a parameter of function a, and the parameter requires a char*. After that, it is transferred from function a to function b, still as a parameter, but this time the parameter requires a QString. And then it is in function b that I want to convert this QString to QChar array.
What did you do exactly? QString is an Unicode string and you have to convert it to QByteArray using some 8-bit encoding before you can access the data as char *.

KaKa
19th March 2007, 00:38
ok...I will try more on that ...Thanks!