Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

UTF-8 decoding library

I have to code in an application which is in Unicode UTF-8 in Windows, MSVC 10. I'm aware that the UTF-8 encoded strings would use either 1 or 2 bytes per character. So, my question is : Is std::string suitable for this? If yes, how do I decode the strings? As far as I understand std::string is just an array of bytes and it doesn't provide any decoding logic. How can I know the logical length of the string? How can I extract logical characters from a string? Are there any libraries which helps me to extract logical characters from the string?

e.g : If I have the string "olé" in std::string, I need to know that the length is 3, but not 4.

Answer*

Draft saved
Draft discarded
Cancel
5
  • std::string makes it really hard to sort strings and test characters in all sorts of ways. Not if you don't need such, that's indeed possible to use UTF-8 strings using std::string. You could also make use of QString (Qt) or CString (MFC...). Commented Jun 25, 2012 at 10:30
  • Thanks @Konrad Rudolph for providing the links. They were useful... From the initial glance, it looks like the Boost.Nowide library doesn't have a stringstream... but, I need to look in detail. Commented Jun 25, 2012 at 10:30
  • 1
    @Alexis For that you can (indeed, must) provide custom comparers. That isn’t restricted to std::string though, it’s even true when working with wide characters due to the presence of combining characters etc. Commented Jun 25, 2012 at 10:36
  • I dont think you can get the count of unicode code points in a string using Boost.NoWide or can you (only if all of them are in the BMP)? I see that Boost.NoWide is useful for I/O, but it does not offer functionality for unicode string handling otherwise. Commented Jun 25, 2012 at 10:56
  • I found this amusing from the article: "Windows C++ programmers are educated that Unicode must be done with ‘widechars’. As a result of this mess, they are now among the most confused ones about what is the right thing to do about text". I'm one of them !! Commented Jun 25, 2012 at 11:07

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /