c++ - 动态计算unicode字符

标签 c++ unicode

对于游戏,我需要处理扑克牌。我想利用Playing cards in 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 字符,您可以基于以下内容。

1F0XY在UTF-8中的逐位编码是:

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);

关于c++ - 动态计算unicode字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008743/

相关文章:

C++11构造函数继承和纯虚方法

c++ - 无法将 sregex_iterator 作为流传递给 cout

c++ - 如何在 QString/QDebug 中使用 Unicode 字符?

css - 通过 CSS 将 unicode 值分配给 webfonts 的正确方法?

java - 使用 UNICODE 或 ASCII 字符代码创建 SQL 查询

c++ - 包含 char 数组的结构

c++ - 为什么当我从向导在 Visual Studio 2013 中创建 C++ 类时,我不需要包含来自 stdafx.h 的 header

delphi - Delphi 形式的 Unicode TRectangle TText

python - 增加对 Python 中 Unicode 的理解 (2.7)

c++ - 如何获取 GDI 句柄列表