将unicode(?)字符直接从源代码写入WriteConsoleOutput [英] Writing unicode(?) character directly from source code to WriteConsoleOutput

查看:43
本文介绍了将unicode(?)字符直接从源代码写入WriteConsoleOutput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WinApi中的 WriteConsoleOutput 将字符写入命令提示符窗口缓冲区.问题是,我真的很希望能够按原样直接将诸如之类的字符写入源代码,而不是使用诸如'\uFFFF''0xFF',因为我不太了解它们(代码页/字符集/等之间的差异)

I'm trying to use WriteConsoleOutput from the WinApi to write characters to the command prompt window buffer. The thing is, I'd really like to be able to write characters such as directly into the source code, as-is, instead of using some kind of encoding/notation like '\uFFFF' or '0xFF', since I don't understand them too well (differences between codepages/character sets/etc.)

下面的代码展示了我的问题的最简单形式.运行此代码不会在命令提示符窗口中显示,而是显示一个问号(?).

The code below showcases the simplest form of my problem. Running this code does not print into the command prompt window, but a question mark (?) instead.

#include <Windows.h>

int main()
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    CHAR_INFO c[1] = {0};
    COORD cS = {1, 1};
    COORD cH = {0, 0};
    SMALL_RECT sr = {0, 0, 0, 0};

    c[0].Attributes = FOREGROUND_INTENSITY;
    c[0].Char.UnicodeChar = '☺';
    WriteConsoleOutput(h, c, cS, cH, &sr);
    Sleep(5000);
    return 0;
}

对于我的代码来说,无论安装/使用的是哪种语言,在所有Windows版本之间均能显示输出是至关重要的. 据我所知(这绝对是最小的)),则需要设置一个特定的代码页(希望该代码页在任何语言的Windows中都可以由命令提示符支持).

It is vital for my code to display output identically between all Windows versions, regardless of the languages installed/used. So to my knowledge (which admittedly is absolutely minimal), I'd need to set a specific codepage (one which would hopefully be supported by the command prompt in any language Windows).

我尝试过:
•从使用 CHAR_INFO.UnicodeChar 更改为 CHAR_INFO.AsciiChar
•摆弄 SetConsoleCP SetConsoleOutputCP 函数,但是我不知道如何利用它们来帮助我解决此问题.
•将 Visual Studio->项目->项目属性..->字符集设置更改为每个可能的值.
•除了上述设置之外,还专门使用 WriteConsoleOutputA WriteConsoleOutputW •将带(/出)签名的源代码文件编码更改为UTF-8.

I've tried:
• Changing from using the CHAR_INFO.UnicodeChar to CHAR_INFO.AsciiChar
• Fiddling around with SetConsoleCP and SetConsoleOutputCP functions, but I haven't got a clue on how to utilize them to help me with this problem.
• Changing the Visual Studio -> Project -> Project properties.. -> Character Set setting to every possible value.
• Using specifically either WriteConsoleOutputA or WriteConsoleOutputW in addition to the aforementioned settings
• Changing the source code file encoding to UTF-8 with(/out) signature.

在我的项目中,我以编程方式将命令提示符字体设置为8x8 Terminal,据我所知,它不支持实际的unicode字符.可用字符显示在此处.这些字符的确包含☺",因此我不确定我的问题是否与unicode有关.我不知道了请帮忙.

In my project I'm programmatically setting the command prompt font to 8x8 Terminal, which to my knowledge does not support actual unicode characters. The available characters are displayed here. Those characters do include '☺', so I'm not entirely sure my question is about unicode. I have no idea anymore. Please help.

推荐答案

C源只能是ascii.如果将非ASCII字符嵌入C源文件中,并且IDE可能会以正确的格式显示它们,但是编译器很可能会以不同的方式对待它们,并且传递给它们的可执行函数仍然可以以不同的方式对待它们.它不是便携式的或不可靠的.但是您可以使用转义序列\ x将任意字节嵌入C字符串中.

C source has to be ascii only. If you embed non-ascii characters in a C source file, and IDE might show them in what appears to be the correct format, but the compiler quite likely treats them differently, and the executable function you pass them to can treat them differently still. It's just not portable or reliable. But you can use the escape sequence \x to embed arbitrary bytes in C strings.

UTF-8适合内部使用,但是Windows API尚不支持它,因此您需要转换为Windows 16位字符(UTF-16差不多但不太完全),以显示扩展字符.但是,您必须确保正在调用Windows API的宽字符版本.大多数采用字符串的Windows API函数都有A和W版本(ascii和wide),以实现二进制向后兼容性.如果您在IDE中查询标识符(进入定义等),您应该会看到您拥有的版本.

UTF-8 is good for internal use, but Windows APIs don't yet support it, so you need to convert to Windows 16 bit chars (UTF-16 nearly but not quite), to display extended characters. However you have to ensure that you are calling the wide character version of the Windows API. Most Windows API functions that take string come in a A and W version (ascii and wide) for binary backwards compatibility. If you query the identifier in the IDE (go to definition etc) you should see which version you have.

这篇关于将unicode(?)字符直接从源代码写入WriteConsoleOutput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆