[SOLVED] Converting from Default Encoding to UTF-8



  • 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:

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



  • 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()



  • 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.



  • 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....



  • Figured out what was wrong.

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


Log in to reply
 

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