动态计算 unicode 字符

Dynamically calculating unicode characters

本文关键字:字符 unicode 计算 动态      更新时间:2023-10-16

对于游戏,我需要处理扑克牌。我想利用 Unicode 中的扑克牌,这基本上归结为U+1F0XY X设置颜色和Y设置牌的面。

我现在需要实现一个函数,该函数返回一个字符串,其中包含表示卡的单个 Unicode 字符。我需要使用什么数据类型而不是占位符unicode_char_t来对 Unicode 字符进行数学运算?

std::string cardToUnicodeChar(uint8_t face, uint8_t color)  {
  unicode_char_t unicodeCharacter = 0x1F000 + (color << 4) + face;
  return std::string(unicodeCharacter);
}

如果这是一个孤立的位置,您打算在其中使用计算的 unicode 字符,则可以基于以下内容。

UTF-8 中1F0XY的按位编码为:

11110000 10111111 100000XX 10XXYYYY

您可以按如下方式构造它:

uint8_t buf[5];
buf[0] = 0xf0;
buf[1] = 0x9f;
buf[2] = 0x80 | (color & 12) >> 2;
buf[3] = 0x80 | (color & 3) << 4 | face & 15;
buf[4] = 0;
return std::string(buf);