0

How to convert a std::wstring to a std::string without losing Polish characters (ąęźćżłó)?

std::string readControlText(HWND window)
{
    wchar_t buf[1024];
    GetWindowText(window, buf, 1024);
    std::wstring tmp(buf);
    return std::string(tmp.begin(), tmp.end());
}

I need std::string for communicating with my server.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 2
    Just guessing here, but I guess that the "communication with [your] server" is done with UTF-8? Well then you need to convert the Windows UTF-16 encoded string to UTF-8. Just putting the UTF-16 encoded bytes into a `char` string is not enough (and does no conversion). – Some programmer dude Jun 03 '18 at 12:15
  • 3
    Converting wide chars to regular chars is a direct loss of information. You will need to convert wide string into appropriate encoding such as multibyte UTF-8. Note that UTF-8 text inside of `std::string` can be only treated as an array of bytes, and operations such as indexing / splitting / concatenation will work incorrectly. – user7860670 Jun 03 '18 at 12:17
  • Possible duplicate of [Convert wstring to string encoded in UTF-8](https://stackoverflow.com/questions/4358870/convert-wstring-to-string-encoded-in-utf-8) – Max Vollmer Jun 03 '18 at 12:27
  • 1
    You have to decide on an encoding. What encoding do you need to use? – David Heffernan Jun 03 '18 at 12:58
  • Possible duplicate of [Converting Code pages in c++](https://stackoverflow.com/questions/25274797/converting-code-pages-in-c) – Robert Andrzejuk Jun 03 '18 at 16:51
  • In Poland codepages 852 and 1250 are used. – Robert Andrzejuk Jun 03 '18 at 16:54
  • You need to convert. We cannot help you with that, since we don't know your source encoding or target encoding. Once you submit that information, this question can be properly closed as a duplicate. This question **has** been asked before, just better. – IInspectable Jun 03 '18 at 18:15

0 Answers0