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] Convert C# code to C++/Qt
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.8k 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 last edited by
    #1

    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:
    @
    ※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‰›‱‭※››‱›‰‰‰‸‱※››‰›‱‭※››‱›‶‰‰‸※››‰›‱‭※‰‰‵‷‱※‵
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      Is original string a Unicode string ?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jyang772
        wrote on last edited by
        #3

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

        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jyang772
              wrote on last edited by
              #6

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

              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