1

I'm asking this question to make sure I'm doing the conversion right and not missing some special case.

This should support special characters as well.

Here's an example program that converts both ways. Is this method correct? Does it cover all special characters?

#include <iostream>
#include <boost/filesystem.hpp>
#include <QString>

int main()
{
    QString MyQPath = "abc/مرحبا/geh"; //initial string with arabic characters
    boost::filesystem::path MyBPath = MyQPath.toStdWString(); //convert to boost path
    std::cout<<MyBPath<<std::endl; //print from boost path, works fine

    QString MyQPath2;
    MyQPath2 = QString::fromStdWString(MyBPath.wstring()); //convert from boost path to QString
    std::cout<<qPrintable(MyQPath2)<<std::endl; //print as UTF-8; works
    return 0;
}

Here's the output (in Linux). Although RTL is messed up, but that's alright because it's a terminal issue. I only need to make sure that the information is preserved and not lost in conversions.

enter image description here

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • Might be worth noting that `std::filesystem` exists in C++17 and that there's no longer a need for `boost::filesystem`, if your compiler supports that. – tambre May 01 '17 at 07:49
  • @tambre Which is why I asked [this question](http://stackoverflow.com/questions/43318493/are-the-experimental-features-of-modern-c-reliable-for-long-term-projects) and people recommended to stay with boost for now. – The Quantum Physicist May 01 '17 at 07:50
  • It is no longer an experimental feature - it's been approved and is in the current C++17 standard draft. I personally use something like `using fs = std::filesystem`. You could probably `#ifdef` it to `std::experimental::filesystem` for slightly older compilers. For example MSVC14 supports it completely in C++17 mode. – tambre May 01 '17 at 07:58
  • I'd also recommend [using UTF8 over UTF16 wherever possible](http://utf8everywhere.org/). – tambre May 01 '17 at 08:03
  • @tambre Thanks for the info on `std::filesystem`. The issue is that `boost::filesystem` uses `wstring` by default for Windows. – The Quantum Physicist May 01 '17 at 08:07
  • 1
    `std::filesystem::path` has conversion functions such as [`u8string()`](http://en.cppreference.com/w/cpp/filesystem/path/string) and many others. See [here](http://en.cppreference.com/w/cpp/filesystem) for full documentation. – tambre May 01 '17 at 08:16
  • @tambre not for Qt. `QString` internally keeps data in unicode. – Dmitry Sazonov May 02 '17 at 07:19
  • @DmitrySazonov What "not for Qt"? `u8string()` method, as the name describes, returns an UTF8 string. – tambre May 02 '17 at 18:38

0 Answers0