システム (Windows 8 OS) にローカル言語のフォントをインストールしました。Windows の文字マップ ツールを通じて、その特定のフォントの文字の Unicode を知ることができました。Cプログラムを介してコマンドラインでこれらの文字を出力したかっただけです。
例: ギリシャ文字のアルファがユニコード u+0074 で表されているとします。
「u+0074」を入力として、C プログラムで英字を出力したい
誰でも私を助けることができますか?
まず、コンソールのプロパティで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-
setmode
stdin
通常、とのデフォルトの変換モードを変更するために使用されstdout
ますが、任意のファイルで使用できます。ストリームのファイル記述子に適用する場合_setmode
は、ストリームで入力または出力操作を実行する前に_setmodeを呼び出します。
関数の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() );
}
いくつかの問題があります。コンソール ウィンドウで実行している場合は、コードを UTF-8 に変換し、ウィンドウのコード ページを 65001 に設定します。または、wchar_t
(Windows では UTF-16) を使用してstd::wostream
、およびコード ページを 1200wchar_t
に設定します。プライベート 32 ビット エンコーディングまたは UTF-32 のいずれかです。)
Cには、ワイド文字を定義するプリミティブ型のwchar_tがあります。strcat->wstrcatのような対応する関数もあります。もちろん、使用している環境によって異なります。Visual Studioを使用している場合は、こちらをご覧ください。