Fail-safe conversion between different character encodings

I need to convert strings from one encoding (UTF-8) to another. The problem is that in the target encoding we do not have all the characters from the source encoding, and the libc iconv (3) function does not work in this situation. I want to be able to do the conversion, but in the output string these problematic characters have been replaced with some character like "?".

The programming language is C or C ++.

Is there a way to solve this problem?

0


a source to share


2 answers


Try adding "// TRANSLIT" or "// IGNORE" to the end of the destination character set string. Note that this is only supported in the GNU C Library.

From iconv_open (3) :



   //TRANSLIT
          When the string "//TRANSLIT" is appended to tocode, translitera‐
          tion is activated.  This means that when a character  cannot  be
          represented  in the target character set, it can be approximated
          through one or several similarly looking characters.

   //IGNORE
          When the string "//IGNORE" is  appended  to  tocode,  characters
          that  cannot  be represented in the target character set will be
          silently discarded.

      

Alternatively, manually skip the character and insert the substitution into the output when you get -EILSEQ from iconv (3) .

+2


a source


A regex based on translatable source ranges used to replace the matching placeholder for any characters that do not match.



0


a source







All Articles