Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9080
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Subject | Re: Converting from std::wstring to UTF-8 std::string |
| Date | 2011-08-19 19:01 +0100 |
| Message-Id | <pan.2011.08.19.18.01.20.58000@nowhere.com> |
| Newsgroups | comp.lang.c++ |
| References | <7ebdb5cb-3555-4657-9b74-c5af9157af1b@r12g2000vbe.googlegroups.com> |
| Organization | Zen Internet |
On Fri, 19 Aug 2011 09:34:36 -0700, Peter Poulsen wrote:
> Is there somebody that can give an example of a generic solution for my
> wchar2utf8() function?
The following function converts a single Unicode character to UTF-8. Note
that wchar_t may or may not be a Unicode character (particularly on
Windows, which can't make up its mind whether wide strings are UCS-2 or
UTF-16).
#include <string>
void unicode2utf(std::string& str, unsigned int c) {
#define ADD(str, c, flags, shift) \
str.push_back((char) (flags | ((c >> shift) & 0x3F)))
if (c < 0x80U)
str.push_back((char) c);
else if (c < 0x800U) {
ADD(str, c, 0xC0, 6);
ADD(str, c, 0x80, 0);
}
else if (c < 0x10000U) {
ADD(str, c, 0xE0, 12);
ADD(str, c, 0x80, 6);
ADD(str, c, 0x80, 0);
}
else if (c < 0x200000U) {
ADD(str, c, 0xF0, 18);
ADD(str, c, 0x80, 12);
ADD(str, c, 0x80, 6);
ADD(str, c, 0x80, 0);
}
else if (c < 0x4000000U) {
ADD(str, c, 0xF8, 24);
ADD(str, c, 0x80, 18);
ADD(str, c, 0x80, 12);
ADD(str, c, 0x80, 6);
ADD(str, c, 0x80, 0);
}
else if (c < 0x80000000U) {
ADD(str, c, 0xFC, 30);
ADD(str, c, 0x80, 24);
ADD(str, c, 0x80, 18);
ADD(str, c, 0x80, 12);
ADD(str, c, 0x80, 6);
ADD(str, c, 0x80, 0);
}
#undef ADD
}
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Converting from std::wstring to UTF-8 std::string Peter Poulsen <pgpoulsen@gmail.com> - 2011-08-19 09:34 -0700
Re: Converting from std::wstring to UTF-8 std::string Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-19 12:44 -0400
Re: Converting from std::wstring to UTF-8 std::string Sam <sam@email-scan.com> - 2011-08-19 11:59 -0500
Re: Converting from std::wstring to UTF-8 std::string Nobody <nobody@nowhere.com> - 2011-08-19 18:21 +0100
Re: Converting from std::wstring to UTF-8 std::string Paavo Helde <myfirstname@osa.pri.ee> - 2011-08-19 12:48 -0500
Re: Converting from std::wstring to UTF-8 std::string Joshua Maurice <joshuamaurice@gmail.com> - 2011-08-19 14:13 -0700
Re: Converting from std::wstring to UTF-8 std::string Marc <marc.glisse@gmail.com> - 2011-08-19 22:09 +0000
Re: Converting from std::wstring to UTF-8 std::string Nobody <nobody@nowhere.com> - 2011-08-19 19:01 +0100
Re: Converting from std::wstring to UTF-8 std::string john <gmlrTOGLI@DELTHISlibero.it.DELCAPS> - 2011-08-19 20:37 +0200
csiph-web