【问题标题】:C++ : Print value of unsigned short variable as unicode characterC ++:将无符号短变量的值打印为unicode字符
【发布时间】:2014-12-07 03:31:06
【问题描述】:

我想将孟加拉语字母打印到控制台,因为该字母的 UTF-16 低 2 个字节存储在 unsigned short 变量中。

例如,对于字母(参考:

unicode 值为 - 0985(hex)

我有,unsigned short unicodeChar = 0x0985;

现在,如何使用unicodeChar 的值打印这封信

注意:我已经尝试过这个-cout << "\u0985";,它可以正确打印

但我希望能够打印变量unicodeChar所代表的字符

【问题讨论】:

  • 使用 wchar_t 代替 unsigned short。
  • 我从二进制文件中读取了这个短值。我试过这样做 - wchar_t wC = unicodeChar; cout

标签: c++ c unicode utf-8 character-encoding


【解决方案1】:

尝试使用 const char* 来存储值。

#include <iostream>

using namespace std;
int main()
{
    const char* uC = "\u0985";
    cout << uC << endl;
}

如果要使用 unsigned short 的值,可以使用setlocale 并通过%lc 打印:

#include <stdio.h>
#include <locale.h>
int main()
{
    if (!setlocale(LC_CTYPE, ""))
    {
        fprintf(stderr, "Error:Please check LANG, LC_CTYPE, LC_ALL.\n");
        return 1;
    }

    unsigned short unicodeChar = 0x0985;
    printf ("%lc\n",unicodeChar);
}

【讨论】:

  • unicodeChar 应该是一个变量。实际上,我从二进制文件中读取了这个变量。之后我必须根据它的值打印一个字符。
  • 使用 setlocale() 怎么样?
猜你喜欢
  • 2014-08-23
  • 2013-06-01
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 2013-05-09
  • 1970-01-01
相关资源
最近更新 更多