Looking if any character whose ascii code is greater than 127

  • Hi,
    I have a QString that perhaps includes some characters whose ASCII code is greater than 127.
    I would like to check.
    I tried using:

    QString test;
    QChar char;
    for (int ascii = 128; ascii <= 255; ascii++)
    char = QChar(ascii);
    if (test.contains(char, Qt::CaseSensitive) ...

    For example, test includes the hex code: 92.
    It is never detected.
    Why ?

    Thank you for your help

  • Lifetime Qt Champion


    Can you provide a sample QString for your test variable ?

  • Worked for me :)

        QString test = QString::fromLatin1("\x80 abc \x90 xyz \xA0");
        QChar chr;
        for (int ascii = 128; ascii <= 255; ascii++)
            chr = QChar(ascii);
            if (test.contains(chr, Qt::CaseSensitive)) {
                qDebug() << "found" << chr;


    found '00\u0080'
    found '00\u0090'
    found '00\u00a0'

    Just paraphrasing @SGaist, how are you setting the test variable?


  • The code looks correct...
    try with:

    if(std::any_of(test.constBegin(),test.constEnd(),[](const QChar& c)->bool{return c.unicode() > 127;})){
    qDebug() << "found > 127";

    QChar char; is invalid. char is reserved, you cannot use it as a variable name

  • Qt Champions 2016

    What about this:

    bool hasLarger = false;
    QString heystack;
    for (QString::ConstIterator i = heystack.constBegin(), end = heystack.constEnd(); i != end; ++i)  { //< Use C++11 range-based if you want
        if (i->unicode() > 127)  {
            hasLarger = true;

    It's practically the same as @VRonin suggested, but the STL is so, so incredibly ugly ...

  • First of all, I generate a QByteArray (m_commandFileContent) from a file:

    // read the command file
    void Command::readCommandFile()
    	QFile file(m_commandFile);
    	m_commandFileContent = file.readAll();
    // accessor to m_commandFileContent
    QString Command::getCommandFileContent()
    	return QString(m_commandFileContent);

    The accessor is used to get the QString.
    Then I apply the tool to search for ASCII code greater than 127:

    bool Tools::searchIncoherentAscii(const QString &content)
    	QChar ch;
    	for (int ascii = 127; ascii <= 255; ascii++)
    		ch = QChar(ascii);
    		if (content.contains(ch, Qt::CaseInsensitive)) return true;
    	return false;

    I made a hardcopy as jpg of the 32 first bytes of the file but I don't know how to paste it here

  • When I drag over content in debug mode in VS, I get this:

    [29] 102 'f'
    [30] 102 'f'
    [31] 65533 'white losange including "?"'

    Hexa editor for [31] gives 92

  • can you post the content of m_commandFile? is it a text or binary file? I think you are doing serialisation wrong. return QString(m_commandFileContent); smells a lot

  • How can I post the content of the file ?

  • We just want to understand what's in that file. is it a .txt file containing text, is it a binary file (image, document, etc)?

  • Here is the start of the file:

    @Comt = Based on
    L : Litolff’s / 1880
    B : Breitkopf / 1890
    P : Peters / 1892

    The trouble is the character ' after Litolff

  • It's a text file with CR and LF (0d 0a) at the end of each line

  • Qt Champions 2016

    If you have a text file, then read it as a text file! Why are all those shenanigans even needed?

    QFile file("C:/path/to/file");
    if (!file.open(QFile::ReadOnly | QFile::Text)) //< Open as text file so you get CL/LF conversions properly
        ; //< Can't open file, handle accordingly
    QTextStream in(&file);
    while (!in.atEnd())  {
        QString line = in.readLine();
        // ... do something with he line ...

  • At the very beginning, I read it as a text file. The problem was that I got LF instead CR LF and I need CR LF to rewrite the file content into another file

  • Qt Champions 2016

    That's because there are line ending conversions done depending on the OS. It's not a problem!
    You read it as a text file, and you get "\n" where the newline is. When you write it as a text file "\r\n" will be written wherever there's "\n". That's the difference between text and binary files.

  • The trouble is that the file I write to is binary because I append text files and binary files

  • It runs if I use this:

    bool Tools::searchIncoherentAscii(const QString &content)
    	int j;
    	for (int i = 0; i < content.length(); i++)
    		j = content.at(i).unicode();
    		if ( j > 127) return true;

  • @mulfycrowh said:

    The problem was that I got LF instead CR LF and I need CR LF to rewrite the file content into another file

    use QString::replace("\n","\r\n");

  • @VRonin Many thanks for your help. I sent you a private message onto your mailbox about another subject. Could you please have a look ?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.