Converting a KOI8-r string for display Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Widgets
Super wx Problem Solver
Super wx Problem Solver
Posts: 349
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Converting a KOI8-r string for display

Post by Widgets » Thu Feb 21, 2019 3:20 am

Recently my mail screener ended up receiving a subject line encoded in KOI8-r and I am stuck trying to convert the string to a displayable string with mixed Cyrillic ASCII characters.
The original string looks like:

Code: Select all

Subject: =?koi8-r?B?7sUg1cTBxdTT0SDEz9PUwdfJ1Ng6IFNQQU0gZnJvbSB5b3VyIG5ldHdvcms/?=
 =?koi8-r?Q?=3F=3F?=
The base64 decoded string is

Code: Select all

7sUg1cTBxdTT0SDEz9PUwdfJ1Ng6IFNQQU0gZnJvbSB5b3VyIG5ldHdvcms/

which is supposed to be displayed as

Code: Select all

Не удается доставить: SPAM from your network?
instead it is displayed as

Code: Select all

"îÅ ÕÄÁÅÔÓÑ ÄÏÓÔÁ×ÉÔØ: SPAM from your network?"
It seems that the KOI8 string does not translate to UTF-8 properly
The code I am trying to use:

Code: Select all

if (wsCharSet.IsSameAs(_T("koi8-r")))
      {
        wxString wsIn = str_content; <<< output from base64 decode = "îÅ ÕÄÁÅÔÓÑ ÄÏÓÔÁ×ÉÔØ: SPAM from your network?"
        wxFontEncoding fontencFrom = wxFontMapper::Get()->CharsetToEncoding( wsCharSet ); 
        wxFontEncoding fontencTo   = wxFontMapper::Get()->CharsetToEncoding(  _T("UTF-8") );
        wxCSConv convFrom( wxFontMapper::GetEncodingName( fontencFrom ) );
        wxCSConv convTo( wxFontMapper::GetEncodingName( fontencTo ));
        wxString wsOut = wxString( wsIn.wc_str( convFrom ), convTo );
      }
Environment: Win 10 64-bit & Ubuntu 16.04
MSVC Express 2010, 2015, 2017
wxWidgets 3.1
Fight SPAM - report it with wxSR

User avatar
doublemax
Moderator
Moderator
Posts: 14088
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Converting a KOI8-r string for display

Post by doublemax » Thu Feb 21, 2019 8:17 am

That looks unnecessarily complicated.

This gives the expected result for me:

Code: Select all

  const char in[] = "îÅ ÕÄÁÅÔÓÑ ÄÏÓÔÁ×ÉÔØ: SPAM from your network?";
  wxString out(in, wxCSConv("koi8-r") );

  wxLogMessage( out );
Use the source, Luke!

Widgets
Super wx Problem Solver
Super wx Problem Solver
Posts: 349
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Converting a KOI8-r string for display

Post by Widgets » Thu Feb 21, 2019 5:48 pm

Thank you.
By using the detected encoding 'wsCharSet', it makes the solution much more general :-)

Code: Select all

wxString out( in, wxCSConv(wsCharSet) );
Environment: Win 10 64-bit & Ubuntu 16.04
MSVC Express 2010, 2015, 2017
wxWidgets 3.1
Fight SPAM - report it with wxSR

Post Reply