SHARE
TWEET

Untitled

a guest Oct 21st, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. string toUTF8(uint32_t cp)
  2. {
  3.     char utf8[4];
  4.     int len = 0;
  5.  
  6.     if (cp <= 0x007F)
  7.     {
  8.         utf8[0] = static_cast<char>(cp);
  9.         len = 1;
  10.     }
  11.     else
  12.     {
  13.         if (cp <= 0x07FF)
  14.         {
  15.             utf8[0] = 0xC0;
  16.             len = 2;
  17.         }
  18.         else if (cp <= 0xFFFF)
  19.         {
  20.             utf8[0] = 0xE0;
  21.             len = 3;
  22.         }
  23.         else if (cp <= 0x10FFFF)
  24.         {
  25.             utf8[0] = 0xF0;
  26.             len = 4;
  27.         }
  28.         else
  29.             throw std::invalid_argument("invalid codepoint");
  30.  
  31.         for (int i = 1; i < len; ++i)
  32.         {
  33.             utf8[len - i] = static_cast<char>(0x80 | (cp & 0x3F));
  34.             cp >>= 6;
  35.         }
  36.  
  37.         utf8[0] |= static_cast<char>(cp);
  38.     }
  39.  
  40.     return std::string(utf8, len);
  41. }
  42.  
  43.  
  44. string utf8 = toUTF8(final_simbol);
  45. cout << utf8 << endl;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top