Antonino INDEVA
Antonino INDEVA

Reputation: 21

How to print 2-byte unicode characters

I'm trying to print this vector which also includes unicode characters:

unsigned short RussianStr[] = { 0x044D, 0x044E, 0x044F, 0x0000};

For this reason I cannot use a vector of char but of unsigned short. How do I print all the vector characters? With the printf () function I only see the first character printed

Upvotes: 2

Views: 1147

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12668

The problem is not how to print utf-16 values.... but if your terminal will print utf at all.

If your terminal is utf capable, then you have only to use the wchar_t alternatives to the printf family of functions... and instead of using char, use wchar_t characters. As terminals are normally byte oriented, a conversion from utf-16 to utf-8 will be made by the locale functions and utf-8 chars will be output.

See wprintf(3) and many others.

Upvotes: 0

David Ranieri
David Ranieri

Reputation: 41017

There are specialized functions and types to deal with Wide characters:

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main(void) 
{
    wchar_t RussianStr[] = {0x044D, 0x044E, 0x044F, 0x0000};

    setlocale(LC_ALL, "");
    wprintf(L"%ls\n", RussianStr);
    return 0;
}

Upvotes: 3

Related Questions