<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QString encoding to std::string]]></title><description><![CDATA[<p dir="auto">I read a lot about encoding the last days, but nothing solved my problem so far.</p>
<p dir="auto">In Qt you can ask the user for a filename/path like:</p>
<pre><code>QString q_filename=QFileDialog::getOpenFileName(...);
</code></pre>
<p dir="auto">If I use now QMethods to open the file with q_filename, everything will be fine. As soon as I like to open the file with CppMethods (due to some external library), I have to transform the filename from QString to std::string:</p>
<pre><code>std::string cpp_filename=q_filename.toStdString();
</code></pre>
<p dir="auto">But now some files won't open, due to special characters/encoding.</p>
<p dir="auto">I can change the encoding for example by using:</p>
<pre><code>std::string cpp_filename=q_filename.toLatin1().toStdString();
</code></pre>
<p dir="auto">I tested several encodings (like toUtf8, toLocal8Bit etc.) and some of them work with my special characters some didn't work.</p>
<p dir="auto">My idea was to simply test it and take a solution that works. However, as soon as I start using std::filesystem::path to perform some operation on the cpp_filename everything starts falling apart and very strange characters appear.</p>
<p dir="auto">My question is now:<br />
How do I know, how I have to transform the encoding. I read a lot about encoding, but everybody assumes, that I know where I come from and where to go. But how do I know this?</p>
<p dir="auto">It seems strange to me, that in 2023 using Windows 10, MinGW and Qt 6.3 this is still problem. I always thought everyone is enjoying utf8, today.</p>
]]></description><link>https://forum.qt.io/topic/143332/qstring-encoding-to-std-string</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 13:13:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/143332.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 01 Mar 2023 10:40:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QString encoding to std::string on Fri, 03 Mar 2023 08:33:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> said in <a href="/post/749855">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto">You are aware of QCoreApplication::arguments(), I hope?</p>
</blockquote>
<p dir="auto">This is something we do in our command line applications that (on purpose) do not use any Qt. (Because of static linking which makes our lives way easier with our clients.)</p>
<p dir="auto">QCoreApplication::arguments() works fine ;-)</p>
]]></description><link>https://forum.qt.io/post/749858</link><guid isPermaLink="true">https://forum.qt.io/post/749858</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Fri, 03 Mar 2023 08:33:45 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Fri, 03 Mar 2023 08:32:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a><br />
I am an old-school C purist.  There is a little wrinkle with your code which is probably irrelevant/you don't care about, but for the sake of completeness you <em>might</em> feel like adding :)</p>
<p dir="auto">Since ANSI C/C 99 it was stipulated that <code>argv[argc]</code> should be <code>NULL</code>.  Code using <code>argv</code> could either look at <code>argc</code> or could iterate till <code>argv[i] == NULL</code> to visit all arguments.  I have searched and cannot find anything which says this is no longer the case(?).  So if you are passing your newly created <code>argv</code> replacement to other code you might verify it enters with <code>argv[argc] == nullptr</code> and then like to <code>malloc((argc + 1) * sizeof(char*))</code> and <code>argv[argc] = nullptr</code>. :)</p>
]]></description><link>https://forum.qt.io/post/749857</link><guid isPermaLink="true">https://forum.qt.io/post/749857</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 03 Mar 2023 08:32:59 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Fri, 03 Mar 2023 08:27:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a> said in <a href="/post/749852">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto">Little caveat: The setlocale trick does not fully solve command line arguments.</p>
</blockquote>
<p dir="auto">You are aware of <code>QCoreApplication::arguments()</code>, I hope?</p>
]]></description><link>https://forum.qt.io/post/749855</link><guid isPermaLink="true">https://forum.qt.io/post/749855</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Fri, 03 Mar 2023 08:27:22 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Fri, 03 Mar 2023 08:16:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> said in <a href="/post/749719">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto">The OP needs a std::string for a 3rd-party library. If the file access is done by the library, and not in his or her own code, it will probably not work on Windows if there are Unicode characters in the file name.</p>
</blockquote>
<p dir="auto">So, then it depends on the library. If it uses fstream internally, this approach will work, as setlocale() will change it for everything. If the library uses Windows' file API directly I would hope that there is a wide string interface as well because otherwise you are out of luck (I guess...).</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> Using the setlocale trick also switches the output via std::cout to UTF-8. You only have to make sure that your editor saves your source code files as UTF-8. And you have to tell the Microsoft compiler that the source is UTF-8 and that it should produce UTF-8 binaries (IIRC two separate switches).</p>
<p dir="auto">Little caveat: The setlocale trick does not fully solve command line arguments. Suddenly, we had problem with certain file names/paths. Here is the work around that we are using for this to truly be fully inside UTF-8:</p>
<pre><code>#ifdef _WIN32
int main(int argc_discard, char** argv_discard)
#else
int main(int argc, char** argv)
#endif
{
#ifdef _WIN32
    setlocale(LC_CTYPE, ".utf8");

    int argc;
    wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &amp;argc);
    char** argv;
    argv = malloc(argc * sizeof(char*));
    for (int i = 0; i &lt; argc; ++i)
    {
        const int size = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);    // get length of string
        argv[i] = malloc(size + 1);
        WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], size + 1, NULL, NULL);           // convert to UTF-8
    }
    LocalFree(wargv);
#endif
   ...
}
</code></pre>
]]></description><link>https://forum.qt.io/post/749852</link><guid isPermaLink="true">https://forum.qt.io/post/749852</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Fri, 03 Mar 2023 08:16:48 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Fri, 03 Mar 2023 06:16:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> It just tells you that all WinAPI calls ending with 'W' are taking an utf-16 string but that was the case already 20 years ago. All non-native stuff which most c(++) programmers use take a std::string or char* and this is encoded in the current locale. Even the console output is printed in ythe current locale instead utf-8 (which is the default since maybe one year, don't even know if it is the default when you just do an upgrade) and therefore all the questions why qDebug() and std::cout don't print the correct characters on a windows console.</p>
]]></description><link>https://forum.qt.io/post/749836</link><guid isPermaLink="true">https://forum.qt.io/post/749836</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 03 Mar 2023 06:16:49 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Thu, 02 Mar 2023 19:42:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said in <a href="/post/749783">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> said in <a href="/post/749638">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto">But on Windows, utf-16  is the native encoding.</p>
</blockquote>
<p dir="auto">This is wrong.</p>
</blockquote>
<p dir="auto">Well, not completely wrong, anyway. This is a quote directly from the "horse's mouth", as it were:</p>
<p dir="auto"><em>"UTF-16 is basically the de facto standard encoding used by Windows Unicode-enabled APIs. UTF-16 is the “native” Unicode encoding in many other software systems, as well. For example, Qt, Java and the International Components for Unicode (ICU) library, just to name a few, use UTF-16 encoding to store Unicode strings."</em></p>
<p dir="auto">Source: <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/september/c-unicode-encoding-conversions-with-stl-strings-and-win32-apis" target="_blank" rel="noopener noreferrer nofollow ugc">https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/september/c-unicode-encoding-conversions-with-stl-strings-and-win32-apis</a></p>
<p dir="auto">By the way, thanks for your extremely helpful reply!</p>
]]></description><link>https://forum.qt.io/post/749799</link><guid isPermaLink="true">https://forum.qt.io/post/749799</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Thu, 02 Mar 2023 19:42:46 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Thu, 02 Mar 2023 18:09:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> said in <a href="/post/749638">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto">But on Windows, utf-16  is the native encoding.</p>
</blockquote>
<p dir="auto">This is wrong.</p>
]]></description><link>https://forum.qt.io/post/749783</link><guid isPermaLink="true">https://forum.qt.io/post/749783</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Thu, 02 Mar 2023 18:09:26 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Thu, 02 Mar 2023 11:10:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a> The OP needs a <code>std::string</code> for a 3rd-party library. If the file access is done by the library, and not in his or her own code, it will probably not work on Windows if there are Unicode characters in the file name.</p>
<p dir="auto">The <code>#ifdef</code> switching should be enough to ensure that it will continue to work normally under Linux, if it was working before:</p>
<pre><code>#ifdef QT_OS_WINDOWS
setLocale(LC_ALL, ".UTF8");
#endif
</code></pre>
]]></description><link>https://forum.qt.io/post/749719</link><guid isPermaLink="true">https://forum.qt.io/post/749719</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Thu, 02 Mar 2023 11:10:01 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Thu, 02 Mar 2023 09:11:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a> said in <a href="/post/749694">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto">We use the approach with <code>setlocale(LC_ALL, ".UTF8")</code> at the beginning of main(). You need to wrap it inside an #ifdef for Windows</p>
</blockquote>
<p dir="auto">I suggested this above to the OP, but they rejected it on the basis of</p>
<blockquote>
<p dir="auto">but unfortunately the suggestion from reddit won't be very useful because the system also has to run under Linux</p>
</blockquote>
<p dir="auto">! :)</p>
]]></description><link>https://forum.qt.io/post/749702</link><guid isPermaLink="true">https://forum.qt.io/post/749702</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 02 Mar 2023 09:11:42 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Thu, 02 Mar 2023 08:46:42 GMT]]></title><description><![CDATA[<p dir="auto">Here is what we do and it has been working without any problems over a year now:</p>
<p dir="auto">We use the approach with <code>setlocale(LC_ALL, ".UTF8")</code> at the beginning of main(). You need to wrap it inside an #ifdef for Windows because it can crash on other systems (I think it was on macOS, but not on Linux). Now, you are in a world where on all systems you can open files using UTF-8 filenames.</p>
<p dir="auto">For a while we had some strange problems with converting QString to std::string. Here is the way we do it now:</p>
<pre><code>QString qstr("...something...");
std::string str = qstr.toUtf8().data();
// and the other way around
qstr = QString::fromUtf8(str.c_str());
</code></pre>
<p dir="auto">This has worked reliably for us.</p>
<p dir="auto">Though, there is technically a slight caveat concerning temporaries. To be on the safe side, you should save the result from toUtf8() to a temporary QByteArray and use data() on this variable. In very few cases we got our app to crash because of this. I think the C++ standard states something like this about temporaries.</p>
]]></description><link>https://forum.qt.io/post/749694</link><guid isPermaLink="true">https://forum.qt.io/post/749694</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Thu, 02 Mar 2023 08:46:42 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Wed, 01 Mar 2023 21:26:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/markus1990">@<bdi>Markus1990</bdi></a> I have had the same problem before. On Linux, the <code>utf-8 </code> encoding should work OK. But on Windows, <code>utf-16 </code> is the native encoding.</p>
<p dir="auto">This page might help:<br />
<a href="https://newbedev.com/how-to-open-an-std-fstream-ofstream-or-ifstream-with-a-unicode-filename" target="_blank" rel="noopener noreferrer nofollow ugc">https://newbedev.com/how-to-open-an-std-fstream-ofstream-or-ifstream-with-a-unicode-filename</a></p>
<p dir="auto">Also, there is <code>QFile::encodeName()</code> and <code>QFile::decodeName()</code> which might help.</p>
]]></description><link>https://forum.qt.io/post/749638</link><guid isPermaLink="true">https://forum.qt.io/post/749638</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Wed, 01 Mar 2023 21:26:37 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Wed, 01 Mar 2023 16:30:20 GMT]]></title><description><![CDATA[<p dir="auto">If you want to use a non-ascii filename under windows with a non Qt api you have to use the encoding currently set for your account. For this you can use <a href="https://doc.qt.io/qt-6/qstring.html#toLocal8Bit" target="_blank" rel="noopener noreferrer nofollow ugc">QString::toLocal8Bit()</a>. But then you won't be able to open a file with characters non representable in your local encoding. Therefore convert <a href="https://doc.qt.io/qt-6/qstring.html#toStdWString" target="_blank" rel="noopener noreferrer nofollow ugc">QString to a std::wstring</a> and use the appropriate system calls which take  a std::wstring instead.<br />
Or even better - stay within Qt.</p>
]]></description><link>https://forum.qt.io/post/749589</link><guid isPermaLink="true">https://forum.qt.io/post/749589</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 01 Mar 2023 16:30:20 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Wed, 01 Mar 2023 13:47:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/markus1990">@<bdi>Markus1990</bdi></a> said in <a href="/post/749561">QString encoding to std::string</a>:</p>
<blockquote>
<p dir="auto">the suggestion from reddit won't be very useful because the system also has to run under Linux</p>
</blockquote>
<p dir="auto">I thought that Linux does use UTF-8 so there won't be the issue there, you are just looking for a solution for Windows.  Anyway I don't know so over to you.</p>
]]></description><link>https://forum.qt.io/post/749563</link><guid isPermaLink="true">https://forum.qt.io/post/749563</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 01 Mar 2023 13:47:43 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Wed, 01 Mar 2023 13:43:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thanks, but unfortunately the suggestion from reddit won't be very useful because the system also has to run under Linux. At the moment I just try to avoid the problem. Since a lot of the naming the user won't see, I can use "safe" filenames.</p>
]]></description><link>https://forum.qt.io/post/749561</link><guid isPermaLink="true">https://forum.qt.io/post/749561</guid><dc:creator><![CDATA[Markus1990]]></dc:creator><pubDate>Wed, 01 Mar 2023 13:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to QString encoding to std::string on Wed, 01 Mar 2023 11:47:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/markus1990">@<bdi>Markus1990</bdi></a><br />
Hello and welcome.</p>
<p dir="auto">My (admittedly limited) understanding is that Windows does <em>not</em> use UTF8, at least for file paths.  Are you saying that with <code>toLatin1()</code> does work while others like <code>toUtf8()</code> do not?</p>
<p dir="auto">I find comments like <a href="https://www.reddit.com/r/cpp_questions/comments/ov1xqw/paths_with_nonascii_chars_on_windows/" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.reddit.com/r/cpp_questions/comments/ov1xqw/paths_with_nonascii_chars_on_windows/</a></p>
<blockquote>
<p dir="auto">std::string is an ANSI string. If you need to support non-ANSI characters, you need to use wstring or u8string (utf-8).</p>
</blockquote>
<p dir="auto">There is also a discussion there about</p>
<blockquote>
<p dir="auto">On newer versions of Windows 10 (v1903 and newer) it is possible to define a new setting called activeCodePage to UTF-8 in the manifest file of your application:</p>
<p dir="auto"><a href="https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests#activecodepage" target="_blank" rel="noopener noreferrer nofollow ugc">https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests#activecodepage</a></p>
</blockquote>
<blockquote>
<p dir="auto">If you are using mingw instead of MSVC, use GCC from ucrt repository. Call setlocale(LC_ALL, ".UTF8") in main() before anything else. After that most C library functions should assume that strings passed as char* are UTF-8. Use std::filesystem::path::u8string() to get UTF-8 encoded file name.</p>
</blockquote>
<p dir="auto">However take this with a pinch of salt, someone else may know better than I what your issue is/what to do about it....</p>
]]></description><link>https://forum.qt.io/post/749553</link><guid isPermaLink="true">https://forum.qt.io/post/749553</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 01 Mar 2023 11:47:19 GMT</pubDate></item></channel></rss>