I have a char with the value C5. I know that C5 in hex corresponds the value 197 in decimal. This number (197) corresponds to a character in ASCII extended. I need to show (printf) or convert C5 in it's corresponding UTF-8 character.
Note that this does not convert it to UTF-8. To convert to UTF-8, in C, use wide to multibyte conversion, if you are using a platform that supports UTF-8 (e.g. Linux, but not Windows)
1 2 3 4 5 6 7 8 9 10 11 12
#include <stdlib.h>
#include <locale.h>
#include <stdio.h>
int main()
{
setlocale(LC_ALL, "en_US.utf8"); // or any other .utf8 locale
wchar_t c = L'\u00c5'; // or = L'\xc5';
char mb[MB_CUR_MAX + 1];
int len = wctomb(mb, c);
mb[len] = '\0';
printf("UTF-8 char: %s\n", mb);
}
197 is not a valid value for a char on most systems (since on most systems, CHAR_MAX == 127), while wchar_t is the type capable of holding any character, including yours.
If I could get some extra help.
Let's say I have the following string "DDD %C5 ir".
I want to print it, but replace %C5 by the UTF8 corresponding character. The % is just to identify the point of UTF8/hex
Assuming you can do string processing in C (which is somewhat tedious, compared to C++), it shouldn't be any more difficult than the samples above, but I am not entirely sure you're stating your goal with sufficient detail:
Do you want to print that string, showing the character Å (U+00C5) instead of the three-character fragment "%C5", or do you want to create a new string that holds the two-byte UTF-8 representation of that character ("\xc3\x85") instead of the three-character fragment "%C5", and which would, therefore, display Å when printed on a UTF-8 enabled terminal?
Hello,
Thank you for coming back.
I want to print that string, showing the character Å (U+00C5) instead of the three-character fragment "%C5".
Can you help, pls