Posts

Showing posts with the label Utf 8

"’" Showing On Page Instead Of " ' "

Answer : So what's the problem, It's a ’ ( RIGHT SINGLE QUOTATION MARK - U+2019) character which is being decoded as CP-1252 instead of UTF-8. If you check the encodings table, then you see that this character is in UTF-8 composed of bytes 0xE2 , 0x80 and 0x99 . If you check the CP-1252 code page layout, then you'll see that each of those bytes stand for the individual characters â , € and ™ . and how can I fix it? Use UTF-8 instead of CP-1252 to read, write, store, and display the characters. I have the Content-Type set to UTF-8 in both my <head> tag and my HTTP headers: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> This only instructs the client which encoding to use to interpret and display the characters. This doesn't instruct your own program which encoding to use to read, write, store, and display the characters in. The exact answer depends on the server side platform / database / progra...

Convert Wstring To String Encoded In UTF-8

Answer : The code below might help you :) #include <codecvt> #include <string> // convert UTF-8 string to wstring std::wstring utf8_to_wstring (const std::string& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; return myconv.from_bytes(str); } // convert wstring to UTF-8 string std::string wstring_to_utf8 (const std::wstring& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; return myconv.to_bytes(str); } What's your platform? Note that Windows does not support UTF-8 locales so this may explain why you're failing. To get this done in a platform dependent way you can use MultiByteToWideChar/WideCharToMultiByte on Windows and iconv on Linux. You may be able to use some boost magic to get this done in a platform independent way, but I haven't tried it myself so I can't add about this option. You can use boost's utf_to_utf converter to get char format to store in std::string. st...

Convert Latin1 Characters On A UTF8 Table Into UTF8

Answer : From what you describe, it seems you have UTF-8 data that was originally stored as Latin-1 and then not converted correctly to UTF-8. The data is recoverable; you'll need a MySQL function like convert(cast(convert(name using latin1) as binary) using utf8) It's possible that you may need to omit the inner conversion, depending on how the data was altered during the encoding conversion. After i searched about an hour or two for this answer. I needed to migrate a old tt_news db from typo into a new typo3 version. I already tried convert the charset in the export file and import it back, but didn't get it working. Then i tried the answer above from ABS and startet a update on the table: UPDATE tt_news SET title=convert(cast(convert(title using latin1) as binary) using utf8), short=convert(cast(convert(short using latin1) as binary) using utf8), bodytext=convert(cast(convert(bodytext using latin1) as binary) using utf8) WHERE 1 You can also convert im...

Create A MySQL Database With Charset UTF-8

Answer : Update in 2019-10-29 As mentions by @Manuel Jordan in comments, utf8mb4_0900_ai_ci is the new default in MySQL 8.0 , so the following is now again a better practice: CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; Answer before 2019-10-29 Note: The following is now considered a better practice (see bikeman868's answer): CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Original answer: Try this: CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_general_ci; For more information, see Database Character Set and Collation in the MySQL Reference Manual. You should use: CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Note that utf8_general_ci is no longer recommended best practice. See the related Q & A: What's the difference between utf8_general_ci and utf8_unicode_ci on Stack Overflow.

Convert Std::string To QString

Answer : QString::fromStdString(content) is better since it is more robust. Also note, that if std::string is encoded in UTF-8, then it should give exactly the same result as QString::fromUtf8(content.data(), int(content.size())) . There's a QString function called fromUtf8 that takes a const char* : QString str = QString::fromUtf8(content.c_str()); Usually, the best way of doing the conversion is using the method fromUtf8, but the problem is when you have strings locale-dependent. In these cases, it's preferable to use fromLocal8Bit. Example: std::string str = "ëxample"; QString qs = QString::fromLocal8Bit(str.c_str());