1

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++?

katinas
  • 432
  • 6
  • 11

2 Answers2

1

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.

  • 2
    I'm trying to print but it doesn't print the symbol in just prints a question mark – katinas Oct 21 '18 at 14:57
  • 1
    @katinas Focus on the goal. Check the bytes in the first file. It could be that you've achieved the goal but you test is faulty. – Tom Blodget Oct 22 '18 at 15:53
  • 1
    @katinas use `xxd`, or similar stuff to check what file really contains –  Oct 22 '18 at 16:19
1

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.

execut4ble
  • 138
  • 8