I have an array of uint32_t
. Each is value representing a Unicode characters. I want to print the array like a string but I'm not able to get that working.
I tried a lot of different things
typedef struct String {
uint32_t *characters;
unsigned long length;
} WRString;
char* WRStringToString(WRString *wstr){
char *string = malloc(sizeof(char) * wstr->length * 4);
int i = 0;
int j = 0;
for (; i < wstr->length; i++) {
string[j++] = wstr->characters[i];
char byte2 = (char)wstr->characters[i] >> 8;
if (byte2) {
string[j++] = byte2;
char byte3 = (char)wstr->characters[i] >> 16;
if (byte3) {
string[j++] = byte3;
char byte4 = (char)wstr->characters[i] >> 24;
if (byte4) {
string[j++] = byte4;
}
}
}
}
return string;
}
Always with
WRString *string; //Characters are 0xD6, 0x73, 0x74, 0x65, 0x72, 0x72, 0x65, 0x69, 0x63, 0x68
I tried:
setlocale(LC_CTYPE,"de_DE.UTF-8");
puts(WRStringToString(string));
Gives \326\377\377\377sterreich
.
wprintf(L"%s",WRStringToString(string));
Gives the same as long as no local is set.
Printing UTF-8 strings with printf - wide vs. multibyte string literals and Printing Unicode Character (stored in variables) in C do not really help me.
Any suggestions?