furskytl
28th October 2011, 03:51
I am really confused about the coding system.And I encounter a problem now.
In the main() , I add this:
QTextCodec::setCodecForCStrings(QTextCodec::codecF orLocale());
QTextCodec::setCodecForTr(QTextCodec::codecForLoca le());
My os is winxp,and I my program will deal with the Chinese;
First,I am programming a program to encode and decode the DES.So I read the QString plaintext from the QTextEdit,and then I get its unicode by using QString::unicode(),convert the uniocde to 0-1 bits string manually,get the ciphertext by encoding the 0-1 string.Now the ciphertext is also 0-1 string,and I convert it into a unicode per 16 bits,for example,"110111100101110" is 28462.Finally,I will get an array of the unicode values(like 28462,38710,57196 and 46205).
Then I convert it to QString using :
QString s;
s.append(QChar(unicode)); // this is a cycle,unicode is a variable: 28462,38710,57196 and 46205
Then I put the QString into a QTextEdit:
qtextEdit->setText(s);
Surly it will display something unreadable.Then I get back the content from the qtextEdit:
const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
However,it diaplays like this:
28462
56
32
10
I make some changes,but it always displays the wrong unicode values except the first one!
What's wrong?How should I do?Thanks very much!
Added after 1 4 minutes:
Now I seem to know something!If I replace the following code:
const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
to
QString temp = qtextEdit->toPlainText();
const QChar * data = temp.unicode();
for(int i = 0; i < temp.size(); i++)
{
qDebug() << data[i].unicode();
}
It works correctly!!!How strange it is!!!What is the difference between temp and qtextEdit->toPlainText()???Maybe it is something about the "implicit sharing"? I really hope that somebody can clasify this confusion!Thanks very much!
In the main() , I add this:
QTextCodec::setCodecForCStrings(QTextCodec::codecF orLocale());
QTextCodec::setCodecForTr(QTextCodec::codecForLoca le());
My os is winxp,and I my program will deal with the Chinese;
First,I am programming a program to encode and decode the DES.So I read the QString plaintext from the QTextEdit,and then I get its unicode by using QString::unicode(),convert the uniocde to 0-1 bits string manually,get the ciphertext by encoding the 0-1 string.Now the ciphertext is also 0-1 string,and I convert it into a unicode per 16 bits,for example,"110111100101110" is 28462.Finally,I will get an array of the unicode values(like 28462,38710,57196 and 46205).
Then I convert it to QString using :
QString s;
s.append(QChar(unicode)); // this is a cycle,unicode is a variable: 28462,38710,57196 and 46205
Then I put the QString into a QTextEdit:
qtextEdit->setText(s);
Surly it will display something unreadable.Then I get back the content from the qtextEdit:
const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
However,it diaplays like this:
28462
56
32
10
I make some changes,but it always displays the wrong unicode values except the first one!
What's wrong?How should I do?Thanks very much!
Added after 1 4 minutes:
Now I seem to know something!If I replace the following code:
const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
to
QString temp = qtextEdit->toPlainText();
const QChar * data = temp.unicode();
for(int i = 0; i < temp.size(); i++)
{
qDebug() << data[i].unicode();
}
It works correctly!!!How strange it is!!!What is the difference between temp and qtextEdit->toPlainText()???Maybe it is something about the "implicit sharing"? I really hope that somebody can clasify this confusion!Thanks very much!