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
Forum Updated to NodeBB v4.3 + New Features

Looking if any character whose ascii code is greater than 127

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 4.9k 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.
  • M Offline
    M Offline
    mulfycrowh
    wrote on 8 Aug 2016, 20:33 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Aug 2016, 22:12 last edited by
      #2

      Hi,

      Can you provide a sample QString for your test variable ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Paul Colby
        wrote on 9 Aug 2016, 00:31 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.

        K 1 Reply Last reply 9 Aug 2016, 09:45
        0
        • V Offline
          V Offline
          VRonin
          wrote on 9 Aug 2016, 06:51 last edited by VRonin 8 Sept 2016, 06:55
          #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
          • P Paul Colby
            9 Aug 2016, 00:31

            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.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 9 Aug 2016, 09:45 last edited by kshegunov 8 Sept 2016, 09:48
            #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 9 Aug 2016, 09:55 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 9 Aug 2016, 10:07 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
                • V Offline
                  V Offline
                  VRonin
                  wrote on 9 Aug 2016, 10:09 last edited by VRonin 8 Sept 2016, 10:10
                  #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 9 Aug 2016, 10:29 last edited by
                    #9

                    How can I post the content of the file ?

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 9 Aug 2016, 10:34 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 9 Aug 2016, 10:37 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 9 Aug 2016, 10:45 last edited by
                          #12

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

                          K 1 Reply Last reply 9 Aug 2016, 10:51
                          0
                          • M mulfycrowh
                            9 Aug 2016, 10:45

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

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 9 Aug 2016, 10:51 last edited by kshegunov 8 Sept 2016, 10:51
                            #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 9 Aug 2016, 10:57 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

                              K V 2 Replies Last reply 9 Aug 2016, 11:03
                              0
                              • M mulfycrowh
                                9 Aug 2016, 10:57

                                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

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 9 Aug 2016, 11:03 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 9 Aug 2016, 11:05 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 9 Aug 2016, 12:19 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
                                      9 Aug 2016, 10:57

                                      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

                                      V Offline
                                      V Offline
                                      VRonin
                                      wrote on 9 Aug 2016, 12:43 last edited by VRonin 8 Sept 2016, 12:43
                                      #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 9 Aug 2016, 12:55
                                      0
                                      • V VRonin
                                        9 Aug 2016, 12:43

                                        @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 9 Aug 2016, 12:55 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

                                        1/19

                                        8 Aug 2016, 20:33

                                        • Login

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