Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1060

How to convert UTF-16 format string to UTF-8 format in C++? [duplicate]

$
0
0

I'm working on displaying text contents using the Cairo library, where I'm provided with retrieved text from a PSD file. The text is encoded in UTF-16 format, but Cairo only supports UTF-8. When attempting to display the text using Cairo and passing the retrieved string directly (i.e., retrievedText.c_str()), nothing is displayed.

Here are the details of the retrieved text:

Text: Dear ImGui and 12 Cairo are two different libraries serving different purposes.

Hex:

00 44 00 65 00 61 00 72 00 20 00 49 00 6d 00 47 00 75 00 69 00 20 00 61 00 6e 00 64 00 20 00 31 00 32 00 20 00 43 00 61 00 69 00 72 00 6f 00 20 00 61 00 72 00 65 00 20 00 74 00 77 00 6f 00 20 00 64 00 69 00 66 00 66 00 65 00 72 00 65 00 6e 00 74 00 20 00 6c 00 69 00 62 00 72 00 61 00 72 00 69 00 65 00 73 00 20 00 73 00 65 00 72 00 76 00 69 00 6e 00 67 00 20 00 64 00 69 00 66 00 66 00 65 00 72 00 65 00 6e 00 74 00 20 00 70 00 75 00 72 00 70 00 6f 00 73 00 65 00 73 00 2e 00 20 00

To successfully display the text using Cairo, I need to convert the UTF-16 encoded string to UTF-8. However, I'm unsure of the correct approach to perform this conversion in C++. Any guidance or suggestions on how to convert a UTF-16 format string to UTF-8 format in C++ would be greatly appreciated.

I attempted to display the retrieved text using Cairo by passing the UTF-16 encoded string directly (retrievedText.c_str()), but no text was displayed. To address this issue, I tried manually converting the UTF-16 string to UTF-8 using various approaches in C++,

Additionally, I tried an alternative approach where I retrieved every second character from the UTF-16 string, assuming each character represented a UTF-8 Unicode code point. However, this method also failed in some cases.

What I Expected:

I expected that converting the UTF-16 string to UTF-8 would allow me to successfully display the text using Cairo.

What Actually Resulted:

Despite attempting different conversion methods, including the alternative approach of retrieving every second character, the text was still not displayed when passing the converted UTF-8 string to Cairo. most of the methods i've used has outputted either incorrect or garbled as followsoutput :UTF-8 String:

䐀愀 洀甀 渀 ㈀䌀椀漀愀攀琀漀搀昀攀攀琀尀 irre evn ifrn upss hl mu spiaiyue o raiggahclue nefcsGI nra-ieapiain iegm

Viewing all articles
Browse latest Browse all 1060

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>