[SOLVED] Convert C# code to C++/Qt



  • I need some help convert this C# code to C++/Qt. It encodes a string to Windows-1252 encoded byte array, then decodes it to a string as if it where UTF-8 encoded.

    This works fine in C#.

    @
    string input = "....";
    input = Encoding.UTF8.GetString(Encoding.GetEncoding(1252).GetBytes(input));
    @

    Output:
    @?>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%

    ???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>%>???>>?>%%
    %???>>%>???>>?>?%%??>>%>???%%????`@

    When I attempt to save this in a file, I get the second output. But when I print 'input' in the console, it appears as such. This is what I need to work with.

    My Qt code:

    @
    QTextEncoder* encoder = QTextCodec::codecForName("Windows-1252")->makeEncoder();
    QTextDecoder *decoder = QTextCodec::codecForName("UTF-8")->makeDecoder();

    QByteArray outputData = encoder->fromUnicode(input);
    input = decoder->toUnicode(outputData,outputData.length());
    

    @

    Output:
    @
    ※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‱›‰‰‰‸‱※››‰›‱‭※››‱›‶‰‰‸※››‰›‱‭※‰‰‵‷‱※‵
    @



  • Is original string a Unicode string ?



  • The original string was encoded with UTF-8.

    @※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‱›‰‰‰‸‱※››‰›‱‭※››‱›‶‰‰‸※››‰›‱‭※‰‰‵‷‱※‵※‵@

    Sorry, this probably seems meaningless to you, but the string is, I believe, obfuscated.

    This is how the string is encoded in C#.

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

    @



  • So, you take UTF-8 string convert it to Windows-1252 and then back to UTF-8
    Is it right?

    [quote]When I attempt to save this in a file, I get the second output. But when I print ‘input’ in the console, it appears as such. This is what I need to work with. [/quote]
    I guess that a console supports UTF-8 and displays UTf-8 characters as they should look. Looks like I don't understand what the problem is.
    What do you expect to see and how do you need to use it?



  • Isn't this question the same as http://qt-project.org/forums/viewthread/45558/ ?



  • [quote author="andreyc" date="1406653506"]So, you take UTF-8 string convert it to Windows-1252 and then back to UTF-8
    Is it right?

    [quote]When I attempt to save this in a file, I get the second output. But when I print ‘input’ in the console, it appears as such. This is what I need to work with. [/quote]
    I guess that a console supports UTF-8 and displays UTf-8 characters as they should look. Looks like I don't understand what the problem is.
    What do you expect to see and how do you need to use it?
    [/quote]

    I expect the unicode for the characters in the output of the Qt program to be the same as that in the C# program.

    EDIT

    facepalm

    I just answered my own question....

    Thank you for helping me take a step back.


Log in to reply
 

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