Windows控制台中的希腊字母

时间:2011-05-29 15:09:47

标签: c windows unicode utf-8 console-application

我正在用C编写程序,当我在cmd.exe中运行它时,我想在菜单中包含希腊字符。有人说为了包含希腊字符,你必须使用类似这样的printf

 printf(charset:IS0-1089:uffe);      

但他们不确定。

有谁知道怎么做?

4 个答案:

答案 0 :(得分:4)

假设Windows,你可以:

此代码打印γειά σου

#include "windows.h"

int main() {
  SetConsoleOutputCP(1253); //"ANSI" Greek
  printf("\xE3\xE5\xE9\xDC \xF3\xEF\xF5");
  return 0;
}

十六进制代码在编码为windows-1253时代表γειά σου。如果使用将数据保存为windows-1253的编辑器,则可以使用文字。另一种方法是使用OEM 737(实际上是DOS编码)或使用Unicode。

我使用SetConsoleOutputCP来设置控制台代码页,但您可以在运行程序之前键入命令chcp 1253

答案 1 :(得分:0)

我认为这可能仅在您的控制台支持希腊语时才有效。您可能想要做的是将字符映射到希腊语,但使用ASCII。对于C#但在C中的想法相同。

  

913至936 =大写希腊字母

     

945至968 =小写希腊字母

在Suite101上阅读更多内容:使用希腊字母和C#:如何在创建C#应用程序时正确显示ASCII代码Suite101.com就此link

答案 2 :(得分:0)

您可以使用printf这样打印unicode字符:

printf("\u0220\n");

这将打印Ƞ

答案 3 :(得分:0)

执行此操作的一种方法是打印宽字符串。不幸的是,Windows需要一些非标准的设置来使这项工作。此代码在#if块内进行设置。

#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>

/* This has been reported not to autodetect correctly on tdm-gcc. */
#ifndef MS_STDLIB_BUGS // Allow overriding the autodetection.
#  if ( _WIN32 || _WIN64 )
#    define MS_STDLIB_BUGS 1
#  else
#    define MS_STDLIB_BUGS 0
#  endif
#endif

#if MS_STDLIB_BUGS
#  include <io.h>
#  include <fcntl.h>
#endif

void init_locale(void)
// Does magic so that wprintf() can work.
{
  // Constant for fwide().
  static const int wide_oriented = 1;

#if MS_STDLIB_BUGS
  // Windows needs a little non-standard magic.
  static const char locale_name[] = ".1200";
  _setmode( _fileno(stdout), _O_WTEXT );
#else
  // The correct locale name may vary by OS, e.g., "en_US.utf8".
  static const char locale_name[] = "";
#endif

  setlocale( LC_ALL, locale_name );
  fwide( stdout, wide_oriented );
}

int main(void)
{
  init_locale();
  wprintf(L"μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽\n");
  return EXIT_SUCCESS;
}

这必须保存为带有BOM的UTF-8,以便旧版本的Visual Studio能够正确读取它。您的控制台也必须设置为等宽的Unicode字体,例如Lucida Console,才能正确显示它。要将宽字符串与ASCII字符串混合使用,标准会将%ls%lc格式说明符定义为printf(),尽管我发现它们无处不在。

另一种方法是将控制台设置为UTF-8模式(在Windows上,使用chcp 65001执行此操作。)然后使用printf(u8"μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽\n");打印UTF-8字符串。 UTF-8是Windows上的二等公民,但通常可以使用。尽管如此,尝试在不设置代码页的情况下运行它,你会得到垃圾。