0

Trying to insert some data from CSV to Firebird table in Qt. DB is in ASCII. When inserting some strings with non-ascii symbols get error:

Cannot transliterate character between character sets

Setting QSqlDatabase::setConnectOptions("ISC_DPB_LC_CTYPE=UTF8;") and converting column to UTF8 (CHARACTER SET UTF8) does not help - same error. Trying to suppress unicode characters with no luck as well:

    QTextCodec *codec = QTextCodec::codecForName("latin1");
    QByteArray encodedString = codec->fromUnicode(str);
    str = QString(encodedString);

QString::toLatin1 () does not suppress characters as well. What solution could be here?

Aleksey Kontsevich
  • 4,671
  • 4
  • 46
  • 101

2 Answers2

1

This piece of code should do what you need:

QString h("Honkäüö?ß#asdfe");

unsigned char * data = (unsigned char*)h.data();

QString result;

for(int i = 0; h.size()*2; i+=2) {
   if(data[i] > 127) {
     result.append("?");
  } else {
     result.append(QChar(data[i]));
  }
}

Here is another, more robust, version:

QString h("Honkäüö?ß#asdfe");
QString result;

for(int i = 0; i < h.size(); ++i) {
   QChar qc = h.at(i);
   unsigned char c = *(unsigned char*)(&qc);
   if(c >= 127) {
     result.append("?");
   } else if (QChar(c).isPrint()) {
     result.append(QChar(c));
   }
}

QString result is just used to show what is extracted. You could copy the data[i] in a char array or a append to a QByteArray.

result is Honk?????????#asdfe

This works well for 16bit characters. 32bit characters result in additional '?'s or other characters.

Aaron
  • 1,181
  • 7
  • 15
0

This code extract any unicode (emojis) from qstring (16 or 32 bits) and toReturn contain only characters from ASCII table ( unicode value less than 256 )

QString cleanQString(QString toClean) {

    QString toReturn="";

    for(int i=0;i<toClean.size();i++){
        if(toClean.at(i).unicode()<256){
            toReturn.append(toClean.at(i));
        }
    }

    return toReturn;


}
Luis Gama
  • 81
  • 1
  • 11