Windows控制台中的希腊字母

5

我正在用C语言编写程序,希望在cmd.exe中运行时菜单中能够显示希腊字符。有人说为了包含希腊字符,必须使用类似于以下代码的printf

 printf(charset:IS0-1089:uffe);      

但是他们不确定。

有人知道如何做吗?


2
等等,这个世界上还有谁在使用MS-DOS? - user142019
1
我怀疑这个问题不是关于MS-DOS,而是在Windows控制台上运行。 - Michael Burr
1
@Michael 你说得对。现在我想了一下,不知道为什么我的书一开始就说 MS-DOS。 - system
3
请编辑问题以正确指定您是指传统的MS-DOS还是最近的Windows DOS框/控制台。 - Chris Stratton
可能是如何在Windows控制台上输出Unicode字符串的重复问题。 - phuclv
显示剩余5条评论
4个回答

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

0

我认为这只有在您的控制台支持希腊语时才能正常工作。您想要做的可能是将字符映射到希腊语,但使用ASCII码。对于C#而言,与C相同的想法。

913至936 =大写希腊字母

945至968 =小写希腊字母

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


DOS和在控制台中运行的应用程序之间有很大的区别。 - vcsjones
正确 - 但重点是使用映射 - 将abc转换为希腊ASCII数字。 - tofutim

0
您可以使用printf像这样打印Unicode字符: printf("\u0220\n"); 这将打印出Ƞ

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;
}

为了让旧版本的Visual Studio正常读取该文件,必须将其保存为带有BOM的UTF-8格式。您的控制台还必须设置为单间距Unicode字体(例如Lucida Console)才能正确显示它。为了在ASCII字符串中混合使用宽字符串,标准定义了%ls%lc格式说明符用于printf(),但我发现这些并不适用于所有情况。

另一种方法是将控制台设置为UTF-8模式(在Windows上,可以使用chcp 65001来实现),然后使用printf(u8"μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽\n");输出UTF-8字符串。在Windows系统中,UTF-8是二等公民,但通常可以正常工作。但是请尝试在不先设置代码页的情况下运行它,您会得到垃圾字符。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接