katinas
katinas

Reputation: 442

c++ how to print utf8 symbol using utf8 hex code?

For example I have utf8 hex code of a symbol which is:

e0a4a0

how can I use this hex code to print this character to a file in c++?

Upvotes: 1

Views: 1160

Answers (2)

execut4ble
execut4ble

Reputation: 128

You need to output each byte separately.

For example if you have:

c386

You need to convert first byte (first two characters "c3") to int, convert to char and output, then second byte ("86") and also convert and output.

Conversion for this particular case ("c386"):

string a="c386";
char b = (a[0] - 'A' + 10) * 16 + a[1] - '0';
char c = (a[2] - '0') * 16 + a[3] - '0';
cout << b << c << endl;

"c3" = 44*16 + 3 = 707, "86" = 8*16 + 6 = 134. Convert those int values to char and output them both and you have your character.

Upvotes: 1

user9303491
user9303491

Reputation:

I see two approaches you can take: first is to use it literally as "ठ" second is to encode it in escape sequence \u or \x (both doesn't work, so I don't have an example)

std::cout<<"ठ\xe0\xa4\xa0"<<std::endl;

But I think that best option for you will be open file in binary mode and simply put those bytes there.

Upvotes: 1

Related Questions