简体   繁体   English

在C中打印Unicode字符

[英]Printing unicode character in C

I got some local language font installed in my system (windows 8 OS). 我的系统(Windows 8 OS)中安装了一些本地语言字体。 Through character map tool in windows, i got to know the unicode for those characters for that particular font. 通过Windows中的字符映射工具,我了解了该特定字体的那些字符的unicode。 All i wanted to print those character in command line through a C program. 我只想通过C程序在命令行中打印这些字符。

For example: Assume greek letter alpha is represented with unicode u+0074. 例如:假设希腊字母alpha用Unicode u + 0074表示。

Taking "u+0074" as an input, i would like my C program to output alpha character 以“ u + 0074”作为输入,我希望我的C程序输出字母字符

Can anyone help me? 谁能帮我?

use the Unicode version of the WriteConsole function. 使用WriteConsole函数的Unicode版本。

also, be sure to store the source code as UTF-8 with BOM, which is supported by both g++ and visual c++ 另外,请确保将源代码存储为带有BOM的UTF-8,g ++和visual c ++都支持


Example, assuming that you want to present a greek alpha given its Unicode code in the form "u+03B1" (the code you listed stands for a lowercase "t"): 例如,假设您要以“ u + 03B1”的形式给出希腊字母的Unicode代码(列出的代码代表小写的“ t”):

#include <stdlib.h>         // exit, EXIT_FAILURE, wcstol
#include <string>           // std::wstring
using namespace std;

#undef UNICODE
#define UNICODE
#include <windows.h>

bool error( char const s[] )
{
    ::FatalAppExitA( 0, s );
    exit( EXIT_FAILURE );
}

namespace stream_handle {
    HANDLE const output     = ::GetStdHandle( STD_OUTPUT_HANDLE );
}  // namespace stream_handle

void write( wchar_t const* const s, int const n )
{
    DWORD n_chars_written;
    ::WriteConsole(
        stream_handle::output,
        s,
        n,
        &n_chars_written,
        nullptr         // overlapped i/o structure
        )
        || error( "WriteConsole failed" );
}

int main()
{
    wchar_t const input[]    = L"u+03B1";
    wchar_t const ch        = wcstol( input + 2, nullptr, 16 );
    wstring const s         = wstring() + ch + L"\r\n";

    write( s.c_str(), s.length() );
}

There are several issues. 有几个问题。 If you're running in a console window, I'd convert the code to UTF-8, and set the code page for the window to 65001. Alternatively, you can use wchar_t (which is UTF-16 on Windows), output via std::wostream and set the code page to 1200. (According the the documentation I've found, at least. I've no experience with this, because my code has had to be portable, and on the other platforms I've worked on, wchar_t has been either some private 32 bit encoding, or UTF-32.) 如果您在控制台窗口中运行,则将代码转换为UTF-8,并将该窗口的代码页设置为65001。或者,您可以使用wchar_t (在Windows上为UTF-16),通过std::wostream并将代码页设置为1200。(至少根据我找到的文档。我对此没有经验,因为我的代码必须是可移植的,并且在其他平台上经过努力, wchar_t已经是某种私有的32位编码或UTF-32。)

First you should set TrueType font (Consolas) in console's Properties. 首先,您应该在控制台的“属性”中设置TrueType字体(Consolas)。 Then this code should suffice in your case - 然后此代码就您而言就足够了-

#include <stdio.h>
#include <tchar.h>

#include <iostream>
#include <string>
#include <Windows.h>
#include <fstream>

//for _setmode()
#include <io.h>
#include <fcntl.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR tch[1];
    tch[0] = 0x03B1; 

    // Test1 - WriteConsole
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE) return 1;
    DWORD dwBytesWritten;
    WriteConsole(hStdOut, tch, (DWORD)_tcslen(tch), &dwBytesWritten, NULL);
    WriteConsole(hStdOut, L"\n", 1, &dwBytesWritten, NULL);

    _setmode(_fileno(stdout), _O_U16TEXT);

    // Test2 - wprintf
    _tprintf_s(_T("%s\n"),tch);
    // Test3 - wcout
    wcout << tch << endl;

    wprintf(L"\x03B1\n");

    if (wcout.bad())
    {
        _tprintf_s(_T("\nError in wcout\n"));
        return 1;
    }
    return 0;
}

MSDN - MSDN-

setmode is typically used to modify the default translation mode of stdin and stdout , but you can use it on any file. setmode通常用于修改stdinstdout的默认转换模式,但是您可以在任何文件上使用它。 If you apply _setmode to the file descriptor for a stream, call _setmode before performing any input or output operations on the stream. 如果将_setmode应用于流的文件描述符,请在对流执行任何输入或输出操作之前调用_setmode。

In C there is the primitive type of wchar_t which defines a wide-character. C语言中有wchar_t的原始类型,它定义了一个宽字符。 There are also corresponding functions like strcat -> wstrcat. 还有相应的功能,例如strcat-> wstrcat。 Of course it depends on the environment you are using. 当然,这取决于您使用的环境。 If you use Visual Studio have a look here . 如果您使用Visual Studio,请在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM