英文:
Does Rider or C# supports all emojis?
问题
I'm building a Console game and i want to print some emojis like: 👾 or ⚔️, but rider doesn´t render them correctly.
这些表情渲染正确:👾
string heroName = $"👾{hero.Name}👾";
而这些不渲染:⚔️☠️
string itemName = $"⚔️{item.Name}⚔️"
我的问题是,是我声明字符串的方式有问题,还是Rider配置中的编码问题需要更改?
英文:
I'm building a Console game and i want to print some emojis like: 👾 or ⚔️, but rider doesn´t render them correctly.
These emojis render correctly:👾
string heroName = $"👾{hero.Name}👾";
while these don't:⚔️☠️
string itemName = $"⚔️{item.Name}⚔️"
My question is, is there a problem in the way I declare the strings, or is it a Rider configuration problem with the encoding that I need to change?
答案1
得分: 0
以下是要翻译的内容:
Disclaimer:
I believe, I have some relevant input on your problem.
I'm not a Unicode expert. I don't really understand it well. Just providing my observations based on facts.
You haven't defined what "rendering correctly" and "rendering incorrectly" mean. Therefore, it's a bit hard to provide you a specific answer.
I assume that the problem is that emojis are printed in the console as
?
characters.
你的问题标题是:
Does Rider or C# supports all emojis?
在Windows控制台中渲染表情符号的问题与Rider或C#无关。
部分解决方案
要正确渲染Unicode表情符号,您需要:
-
设置输出的编码并打印表情符号:
Console.OutputEncoding = Encoding.Unicode; Console.WriteLine("👾⚔☠"); // 没有变化 Console.WriteLine("👾⚔️☠️"); // 来自原问题
-
使用 Windows终端 运行您的应用程序:
在CMD中渲染表情符号的问题与当前不支持字体回退的GDI渲染引擎有关。
下面是其他相关的观察:
不清楚您从哪里获取表情符号。
-
您提供的第一个表情符号是正确的。
- 复制它:👾
- 转到 完整的表情符号列表,v15.0
- 按下
Ctrl + F
,粘贴它 - 它被找到为 U+1F47E –
外星怪物
-
第二个表情符号不同,您无法在 完整的表情符号列表,v15.0 中找到它。
但是,您可以找到一个
交叉剑
的表情符号。它在该页面上看起来与您的表情符号完全相同:但是在我的浏览器上,当前的StackOverflow页面上的表情符号看起来不同:⚔
这仅仅是因为使用了字体
Segoe UI Emoji
,所以它在“完整的表情符号列表,v15.0”页面上看起来像一张彩色图片。
我相信,这些将在更多情况下起作用:
- 👾
- ⚔
- ☠
在Windows 10上的Visual Studio 2022中,它们看起来是这样的:
但当我运行应用程序时,在控制台中它们看起来像这样:
我认为您将这种结果称为“未正确渲染”。
默认情况下,控制台输出的编码设置为 OSEncoding
:
因此,您需要将其更改为 Encoding.Unicode
。但在CMD中仍然不起作用。
如果您在Windows终端中运行它,它将以不同的方式呈现文本,因此它将起作用。
但它将根据使用的字体而异。
其他参考资料:
-
StackOverflow的Peter Duniho的答案,标题为“如何在C#控制台输出中显示表情符号?”
英文:
> Disclaimer:
>
> 1. I believe, I have some relevant input on your problem.
>
> I'm not a Unicode expert. I don't really understand it well. Just providing my observations based on facts.
>
> 2. You haven't defined what "rendering correctly" and "rendering incorrectly" mean. Therefore, it's a bit hard to provide you a specific answer.
>
> I assume that problem is that emojis are printed in the console as ?
characters.
Title of your question is:
> Does Rider or C# supports all emojis?
Problem of rendering emojis in Windows Console has nothing to do with Rider or C#.
Partial solution
To render Unicode emojis correctly you need to:
-
Set encoding for the output and print emojis:
Console.OutputEncoding = Encoding.Unicode; Console.WriteLine("👾⚔☠"); // Without variations Console.WriteLine("👾⚔️☠️"); // From the original question
-
Use Windows Terminal to run your app:
> The problem of rendering emojis in CMD (without Windows Terminal) has something to do with GDI rendering engine that currently doesn't support font-fallback.
Additional fragmented observations below:
It is unclear where did you get your emojis from.
-
The first emoji that you provided is OK.
- Copy it: 👾
- Go to Full Emoji List, v15.0
- Press
Ctrl + F
, paste it - It's found as U+1F47E –
Alien monster
-
The second emoji is different, you can't find it at Full Emoji List, v15.0
However, you can find a
Crossed swords
one. It looks exactly like yours on that page:But emoji from that list looks differently in my browser on current StackOverflow page: ⚔
It looks like a colored picture on my Windows machine on the "Full Emoji List, v15.0" page just because of the font
Segoe UI Emoji
.
If we convert your emojis from the question to bytes, we've got:
If you google ef b8 8f
, you'll find a mysterious Variation Selector-16
at the UTF-8 code page (#66).
So, if you remove these extra bytes (ef b8 8f
) of Variation Selector-16
from e2 98 a0 ef b8 8f
, you'll end up with bytes e2 98 a0
which you can encode back to a Unicode character ☠
(link). It looks more simplistic in my browser.
> It has something to do with Surrogate pairs and variation selectors (Microsoft Learn).
I believe, these will work in more cases than original ones:
- 👾
- ⚔
- ☠
In Visual Studio 2022 on Windows 10 they looks that way:
But when I run the app, in console they look like this:
I'd assume that you refer to such a result as "not rendered correctly".
By default, console output encoding is set to OSEncoding
:
So, you need to change it to Encoding.Unicode
. But it still won't work in CMD.
If you run it in Windows Terminal, it renders text differently, therefore, it will work.
But it will work differently depending on the font being used.
Additional references:
-
StackOverflow answer from Peter Duniho for "How to show emoji in c# console output?"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论