C++ : Print value of unsigned short variable as unicode character

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

鉴于 UTF-16,我想在控制台上打印一封孟加拉语字母该字母的低 2 个字节存储在 unsigned short 中变量。

例如,对于字母 (引用:)

unicode 值为 - 0985(十六进制)

我有,unsigned short unicodeChar = 0x0985;

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

注意:我试过了- cout << "\u0985";并打印出

但我希望能够打印由变量 unicodeChar 表示的字符

最佳答案

尽量使用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);
}

关于C++ : Print value of unsigned short variable as unicode character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26325227/

相关文章:

c++ - 使用 POCO JSON 进行增量解析

c - scanf 多个变量或一个变量

php - Unicode字符插入数据库后变成问号

PHP 显示 ???当我从 mysql 显示希伯来语时而不是希伯来语

c++ - 跟踪C++程序执行的工具

c++ - 自动文件重命名 - const 变量正在改变它的值?

c++ - Winsock 速率限制

c - c中的逻辑与和或

C:学习使用指针、头文件等在 C 中编写代码

python - 将 unicode 转换为日期时间格式