1

システム (Windows 8 OS) にローカル言語のフォントをインストールしました。Windows の文字マップ ツールを通じて、その特定のフォントの文字の Unicode を知ることができました。Cプログラムを介してコマンドラインでこれらの文字を出力したかっただけです。

例: ギリシャ文字のアルファがユニコード u+0074 で表されているとします。

「u+0074」を入力として、C プログラムで英字を出力したい

誰でも私を助けることができますか?

4

4 に答える 4

1

まず、コンソールのプロパティでTrueTypeフォント(Consolas)を設定する必要があります。次に、このコードで十分です-あなたの場合-

#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-

setmodestdin通常、とのデフォルトの変換モードを変更するために使用され stdoutますが、任意のファイルで使用できます。ストリームのファイル記述子に適用する場合 _setmodeは、ストリームで入力または出力操作を実行する前に_setmodeを呼び出します。

于 2013-02-19T11:16:15.003 に答える
1

関数のUnicodeバージョンを使用しWriteConsoleます。

また、ソースコードをBOM付きのUTF-8として保存してください。これは、g++とVisualC++の両方でサポートされています。


たとえば、Unicodeコードを「u + 03B1」の形式で指定してギリシャ文字を表示するとします(リストしたコードは小文字の「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() );
}
于 2013-02-19T10:14:19.127 に答える
1

いくつかの問題があります。コンソール ウィンドウで実行している場合は、コードを UTF-8 に変換し、ウィンドウのコード ページを 65001 に設定します。または、wchar_t(Windows では UTF-16) を使用してstd::wostream、およびコード ページを 1200wchar_tに設定します。プライベート 32 ビット エンコーディングまたは UTF-32 のいずれかです。)

于 2013-02-19T10:19:57.170 に答える
0

Cには、ワイド文字を定義するプリミティブ型のwchar_tがあります。strcat->wstrcatのような対応する関数もあります。もちろん、使用している環境によって異なります。Visual Studioを使用している場合は、こちらをご覧ください。

于 2013-02-19T10:15:42.243 に答える