View Full Version : Accented Character

19th February 2011, 15:38
Hello everyone.

I migrated my files (.txt) from Windows (ISO-8859) to Linux (UTF-8).

Replace command is not recognizing the accented character.

QFile file(fileName);
QTextStream in(&file);
QString str = in.readAll().trimmed();

const char tA[4] = {'á','â','ã','à '};
for (int j = 0; j < (int)sizeof(tA); j++) str.replace(tA[j],"a");

Does anyone know what command I have to use or add?


19th February 2011, 18:50
I've done something like this recently, but I'd used the map (you are using an array, for me map was just convenient) where I'd put the keys as the strings (these were polish native letters) and values like in your case - simple English chars (the chars as values in the map as well)

the key in this issue was holding the keys (diacritics) as QString not char, if it was char my method was
unable to recognize diacritic and replace it

in that way it worked, my method successfully recognizes diacritics and replace them using appropriate value

21st February 2011, 16:49

Remember that your source code must be written using ISO-8859, or your code won't work. You have to compare characters written in the same code. Another way, is to convert every thing to the same character code. See QString (http://doc.qt.nokia.com/latest/qstring.html) in your docs.

24th February 2011, 22:25
Thanks for the help. I solved using the ASCII code.

//UTF-8 ASCII Table
const char tA[4] = {225,226,227,228}; //á,â,ã,Ã
const char tE[2] = {233,234}; //é,ê
const char tI[1] = {237}; //Ã*
const char tO[3] = {243,244,245}; //ó,ô,õ
const char tU[2] = {250,252}; //ú,ü
const char tC[1] = {231}; //ç

for (unsigned int j = 0; j < sizeof(tA); j++) str.replace(tA[j],"a");
for (unsigned int j = 0; j < sizeof(tE); j++) str.replace(tE[j],"e");
for (unsigned int j = 0; j < sizeof(tI); j++) str.replace(tI[j],"i");
for (unsigned int j = 0; j < sizeof(tO); j++) str.replace(tO[j],"o");
for (unsigned int j = 0; j < sizeof(tU); j++) str.replace(tU[j],"u");
for (unsigned int j = 0; j < sizeof(tC); j++) str.replace(tC[j],"c");