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] Converting from Default Encoding to UTF-8
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Converting from Default Encoding to UTF-8

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.9k Views 1 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.
  • J Offline
    J Offline
    Jyang772
    wrote on 28 Jul 2014, 16:30 last edited by
    #1

    Here is a C# function that I would like to implement using Qt. It reads a default encoded string from the Windows registry.

    @
    public static string decrypt(string input)
    {
    input = Encoding.UTF8.GetString(Encoding.Default.GetBytes(input));

        string toreturn = "";
    
        for (int i = input.Length - 1; i >= 0; i--)
        {
            toreturn += (char) ((input[i] - 32) % 255);
        }
    
        return toreturn;
    }
    

    @

    Here is what I have written in Qt:

    @
    //This string is taken directly from the registry value.

    QString input = "※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‱›‰‰‰‸‱※››‰›‱‭※››‱›‶‰‰‸※››‰›‱‭※‰‰‵‷‱※‵※‵";
    QString output;

    for(int i=input.length()-1; i>=0; i--){
        output += (char)((input[i].digitValue()-32) % 255);
    }
    
    qDebug() << output;
    

    @

    The output should be:
    @5;5;17500;-1:0::;8006:1::;-1:0::;18000:1::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;-1:0::;@

    But I am getting this:

    @
    ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß
    @

    1 Reply Last reply
    0
    • H Online
      H Online
      hskoglund
      wrote on 28 Jul 2014, 22:05 last edited by
      #2

      Hi, your input string is 570 characters long and the correct output is 190 characters, for each correct character you've got 2 extra UTF-8 overhead chars. Also the bits have moved because of the UTF-8 encoding, instead of subtracting 32 you should subtract 128. Try this:
      @
      for(int i=input.length()-1; i>=0; i = i - 3){
      output += (char)((input[i].cell()-128) % 255);
      }
      @
      Also, cannot use digitValue(), you have to use cell()

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jyang772
        wrote on 29 Jul 2014, 01:24 last edited by
        #3

        Thank you, this works perfectly.

        However, would I encode the output back to it's original input? Since each character was supposed to be shifted by 32 and then modded by 255. I don't fully understand why I am subtracting 128 from the decryption function.

        This was the original C# function for encrypting a string.
        @
        string toreturn = "";

            for (int i = input.Length - 1; i >= 0; i--)
            {
                toreturn += (char) (input[i] + 32 + 8160);
            }
        
            toreturn = Encoding.Default.GetString(Encoding.UTF8.GetBytes(toreturn));
            return toreturn;
        }
        

        @

        I'm not familiar with C#, but I'm guessing this part:

        @
        Encoding.Default.GetString(Encoding.UTF8.GetBytes(toreturn));
        @

        Takes the string and converts it to UTF-8.

        1 Reply Last reply
        0
        • H Online
          H Online
          hskoglund
          wrote on 29 Jul 2014, 04:09 last edited by
          #4

          Hmm looking at the original C# function I see that the bits weren't moved around, the encoding is trivial:
          @
          toreturn += (char) (input[i] + 32 + 8160);
          @

          which is the same as

          @
          toreturn += (char) (input[i] + 2**13);
          @

          That explains why you so easy can retrieve it, i.e. tossing the top 2 bytes and subtracting with 128 gives you back the original.

          But if you really shifted by 32 (instead of adding 32) means you cannot use Unicode for storing your characters anyway since Unicode has only 1114112 characters :-(

          So maybe look at the original code again, could be a bug....

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jyang772
            wrote on 1 Aug 2014, 04:42 last edited by
            #5

            Figured out what was wrong.

            I was supposed to use .unicode() instead of .cell(). Fixed everything! Thank you for the help though.

            1 Reply Last reply
            0

            1/5

            28 Jul 2014, 16:30

            • Login

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