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. [Solved] Can I include extended ascii codes in my application?

[Solved] Can I include extended ascii codes in my application?

Scheduled Pinned Locked Moved General and Desktop
18 Posts 3 Posters 9.4k Views
  • 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
    mlong
    wrote on last edited by
    #4

    Do you really want the entire ASCII table, or do you just want the 7-bit printable characters? If you want the latter, then you'll need to do some more clever rotation than just adding values.

    Assuming the printable characters are 32 (SPACE) - 126 (Tilde) then you could do something like:

    @
    char val;
    char offset;

    if (val >= 32 && val <= 176)
    {
    val -= 32; // Shift the working range from [32-126] -> [0->94] for ease of calculations
    val += offset;
    if (val < 0) { val += 94; } // Offset could be negative, I suppose.
    if (val > 94) { val -= 94; } // Keep the shifted values in range. --
    val += 32 // Shift the character set back into the printable range
    }

    @

    Just brain to keyboard. Your mileage may vary.

    [Edit: Tilde is 126, not 176.]

    Software Engineer
    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #5

      Additionally, you might consider printing the values in hex in your text item, rather than in decimal, as the hex values should ideally all be 2 characters instead of 1 to 3 characters.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        croussou
        wrote on last edited by
        #6

        I suppose that should be placed inside the loop?

        Regards,

        croussou

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

          I would put this functionality into its own method, then call the method from inside your loop.
          @
          char MyClass::convertCharacter(char val, char offset) { }
          @
          or something like that.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            croussou
            wrote on last edited by
            #8

            Got it, thank you!

            Regards,

            croussou

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

              No problem. The nice part is that you can then use that same method to convert back the other way by just using a negative offset.

              _Edit to add: _

              Also, I used chars, but you might want to use a temporary int (or short) variable for the calculations to reduce the possible effects of overflow, should the offset be too high or low.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                croussou
                wrote on last edited by
                #10

                My code now looks like this...

                @
                key = 20;
                input_string = ui->lineEdit->text();

                for(int i = 0; i &lt; input_string.length(); i++)
                {
                    random = input_string.at(i).toAscii();
                    //result1.append(QChar(random).toAscii());
                
                    if ((random) &gt;= 32 && (random) <= 176)
                    {
                        random -= 32;
                        random += key;
                
                        if (random < 0)
                            random += 144;
                
                        if (random > 144)
                                random -= 144;
                
                        random += 32;
                    }
                
                    result1.append(QChar(random).toAscii());
                    ui->lineEdit_3->setText(result1);
                

                @

                It works when the key is small but it malfunctions when the key gets bigger, i.e. 20. When the key is big, it still provides an output, but when you try to select it, then it crashes...I guess there are some 'funny' characters that cannot be read...Because when I copy it and paste it somewhere else it displays this:

                Ž{y†{y†{

                In addition, I noticed it malfunctions most of the time with letter 'z'...no clue.

                Regards,

                croussou

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  croussou
                  wrote on last edited by
                  #11

                  Yes, anything equal or more than 5 causes a problem if the letter 'z' is used...

                  Regards,

                  croussou

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

                    It would have helped if I had used the decimal value for ~ (126) rather than the octal value (0176).

                    So change all the 176s to 126, and change all the 144s to 94.

                    I edited my earlier example to reflect this correction.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #13

                      The following works for me:

                      @

                      #include <QDebug>
                      #include <QString>

                      int main(int argc, char** argv)
                      {
                      int key = 20;

                      QString input_string;
                      for (int idx = ' '; idx <= '~'; idx++)
                      {
                         input_string.append(QChar(idx));
                      }
                      
                      qDebug() <&lt; input_string;
                      
                      QString result1;
                      
                      int random = 0;
                      
                      for (int i = 0; i &lt; input_string.length(); i++)
                      {
                          random = input_string.at(i).toAscii();
                      
                          if ((random) >= 32 && (random) <= 126)
                          {
                              random -= ' ';
                              random += key;
                      
                              while (random < 0) random += 94;
                              while (random > 94) random -= 94;
                      
                              random += ' ';
                          }
                          result1.append(QChar(random).toAscii());
                      } 
                      
                      qDebug() << result1;
                      
                      return 0;
                      

                      }
                      @

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        croussou
                        wrote on last edited by
                        #14

                        Already done that and works perfectly. Tested up to key 20...do you think I can go further? or I would start getting crashes?

                        Regards,

                        croussou

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mlong
                          wrote on last edited by
                          #15

                          Change the two inner ifs to whiles, and then it should always keep your resultant strings within bounds (for instance if the offset were > 94).

                          As for crashes, see for yourself. And if you were to crash, it would be a good opportunity to practice debugging.

                          Software Engineer
                          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            croussou
                            wrote on last edited by
                            #16

                            Seems good thank you.

                            For now, I will keep the key small and maybe later develop this further.

                            Anyhow, thanks again!

                            Regards,

                            croussou

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lgeyer
                              wrote on last edited by
                              #17

                              One small addition: as you know the length of the resulting string in advance you should reserve it using QString::reserve(). This will save, depending on the length of the string, a bunch of reallocations, which might or will have an impact on your performance, especially if the "transformation function" is called often.

                              1 Reply Last reply
                              0
                              • C Offline
                                C Offline
                                croussou
                                wrote on last edited by
                                #18

                                Good suggestions indeed, but that function will not be called that often. Just once actually for the user to enter the message and get it 'encrypted'.

                                Thanks for the suggestion though.

                                Regards,

                                croussou

                                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