Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Looking if any character whose ascii code is greater than 127
Qt 6.11 is out! See what's new in the release blog

Looking if any character whose ascii code is greater than 127

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 7.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Paul ColbyP Offline
    Paul ColbyP Offline
    Paul Colby
    wrote on last edited by
    #3

    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;
            }
        }
    

    Output:

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

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

    Cheers.

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #4

      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";
      }
      

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        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;
                }
            }
        

        Output:

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

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

        Cheers.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #5

        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;
                break;
            }
        }
        

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

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • M Offline
          M Offline
          mulfycrowh
          wrote on last edited by
          #6

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

          // read the command file
          void Command::readCommandFile()
          {
          	QFile file(m_commandFile);
          	file.open(QIODevice::ReadOnly);
          	m_commandFileContent = file.readAll();
          	file.close();
          }
          
          // 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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mulfycrowh
            wrote on last edited by
            #7

            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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #8

              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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mulfycrowh
                wrote on last edited by
                #9

                How can I post the content of the file ?

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #10

                  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)?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mulfycrowh
                    wrote on last edited by
                    #11

                    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

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mulfycrowh
                      wrote on last edited by
                      #12

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

                      kshegunovK 1 Reply Last reply
                      0
                      • M mulfycrowh

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

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #13

                        @mulfycrowh
                        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 ...
                        }
                        

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        1
                        • M Offline
                          M Offline
                          mulfycrowh
                          wrote on last edited by
                          #14

                          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

                          kshegunovK VRoninV 2 Replies Last reply
                          0
                          • M mulfycrowh

                            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

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #15

                            @mulfycrowh
                            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.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              mulfycrowh
                              wrote on last edited by
                              #16

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

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mulfycrowh
                                wrote on last edited by
                                #17

                                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;
                                	}
                                
                                1 Reply Last reply
                                0
                                • M mulfycrowh

                                  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

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by VRonin
                                  #18

                                  @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");

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  M 1 Reply Last reply
                                  0
                                  • VRoninV VRonin

                                    @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");

                                    M Offline
                                    M Offline
                                    mulfycrowh
                                    wrote on last edited by
                                    #19

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

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved