Questions about std::stringstream



  • Other question: how to pass std::string object to the WinAPI function which will write data to it?



  • SAn schrieb:

    Other question: how to pass std::string object to the WinAPI function which will write data to it?

    Hm, I think you can't do this directly since the internal stuff of std::string is implementation-defined.

    So, you have to go the indirect way about a char -Pointer. Getting a string's Pointer can be done by the member function std::string::c_str() . Note that you get a const char* here, so you have to copy the string.

    Assigning a std::string to a char -Pointer can either be done by operator= , assign() , or the constructor.



  • I assume that you need to pass a char*. You could do this by copying the whole string via std::string::copy(char* s, size_t n) to a char-array. Or you directly use char*. It depends on what you aim to do with the string.





  • Nice. Now I was the first to find the probably more suitable method 😉

    Have a nice day. I should work a little bit now...



  • Question:

    Will std::basic_stringstream<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> > correctly process UTF32? Will operator>> will convert it to numbers etc. ??



  • Der stellt ja immer nur Fragen ohne auf die Antworten einzugehen.

    Allerunterste Schublade. 😡



  • Will std::basic_stringstream<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> > correctly process UTF32?
    

    Of course. But it will not provide you anything more than simple input-output of 4-byte strings. If you want to do any conversions from or to any encoding you have to implement it by yourself.

    Will operator>> will convert it to numbers
    

    Nobody knows, I guess. Because it depends on the implementation of that operator that you will have to supply yourself or get from some library.

    Maybe it would be a good idea to post what you are attempting to do. I don't think there are WinAPI-Functions using UCS4-Encoding. Not even utf16. As far as I know windows is uses UCS2 - or did I miss something?



  • I just sitting here for 4 hours and trying to convert std::vector<unsigned int> containing UTF-32 to the system-default ANSI codepage.

    The only solution I found is:

    1. Convert UTF-32 to UTF-8 using utfcpp.
    2. Convert UTF-8 to UTF-16 using utfcpp or MultiByteToWideChar WinAPI function
    3. Convert UTF-16 to the system-default ANSI codepage by WideCharToMultiByte(CP_ACP,...

    Is there the easier way to do such a conversion???



  • Of course. You should rather call it UCS4 than UTF32. Both is simply pure unicode, one character per 4-byte value.The Winapi-Function

    int WideCharToMultiByte(
      UINT CodePage, 
      DWORD dwFlags, 
      LPCWSTR lpWideCharStr,
      int cchWideChar, 
      LPSTR lpMultiByteStr, 
      int cbMultiByte,
      LPCSTR lpDefaultChar,    
      LPBOOL lpUsedDefaultChar
    );
    

    takes a pointer to a WCHAR-String as its parameter LPCWSTR. You could copy your 4-byte values to a new WCHAR-array and pass that array to WideCharToMultiByte. If some character doesn't fit into 2 bytes, you won't be able to encode it as ANSI anyway. But you could check for overflows and treat the errors as needed in your context.


Anmelden zum Antworten