【发布时间】:2013-01-17 09:49:48
【问题描述】:
我对编程相当陌生,但似乎π(pi) 符号不在ASCII 处理的标准输出集中。
我想知道是否有办法让控制台输出π 符号,从而表达有关某些数学公式的准确答案。
【问题讨论】:
-
你的控制台字体是什么?只需使用 CMD.EXE 检查。它因 Windows 版本而异,可以自定义。
标签: c++ winapi unicode ascii pi
我对编程相当陌生,但似乎π(pi) 符号不在ASCII 处理的标准输出集中。
我想知道是否有办法让控制台输出π 符号,从而表达有关某些数学公式的准确答案。
【问题讨论】:
标签: c++ winapi unicode ascii pi
我不太确定任何其他方法(例如使用 STL 的方法),但您可以使用 WriteConsoleW 在 Win32 上执行此操作:
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
LPCWSTR lpPiString = L"\u03C0";
DWORD dwNumberOfCharsWritten;
WriteConsoleW(hConsoleOutput, lpPiString, 1, &dwNumberOfCharsWritten, NULL);
【讨论】:
Microsoft CRT 不是很熟悉 Unicode,因此可能需要绕过它并直接使用 WriteConsole()。我假设您已经为 Unicode 编译,否则您需要明确使用 WriteConsoleW()
【讨论】:
我正处于学习阶段,如果我有错误,请纠正我。
这似乎是一个三步过程:
您现在应该可以摇滚那些时髦的 åäös。
例子:
#include <iostream>
#include <string>
#include <io.h>
// We only need one mode definition in this example, but it and several other
// reside in the header file fcntl.h.
#define _O_WTEXT 0x10000 /* file mode is UTF16 (translated) */
// Possibly useful if we want UTF-8
//#define _O_U8TEXT 0x40000 /* file mode is UTF8 no BOM (translated) */
void main(void)
{
// To be able to write UFT-16 to stdout.
_setmode(_fileno(stdout), _O_WTEXT);
// To be able to read UTF-16 from stdin.
_setmode(_fileno(stdin), _O_WTEXT);
wchar_t* hallå = L"Hallå, värld!";
std::wcout << hallå << std::endl;
// It's all Greek to me. Go UU!
std::wstring etabetapi = L"η β π";
std::wcout << etabetapi << std::endl;
std::wstring myInput;
std::wcin >> myInput;
std:: wcout << myInput << L" has " << myInput.length() << L" characters." << std::endl;
// This character won't show using Consolas or Lucida Console
std::wcout << L"♔" << std::endl;
}
【讨论】: