【问题标题】:In C# how to get minimum and maximum value of char printed in Unicode format?在 C# 中如何获取以 Unicode 格式打印的 char 的最小值和最大值?
【发布时间】:2018-06-02 04:42:36
【问题描述】:

根据MSDN,char的最小值是U+0000,char的最大值是U+ffff

我已经编写了以下代码来打印相同的内容:

using System;
using System.Collections.Generic;
using System.Linq;

namespace myApp {
    class Program {
        static void Main() {
            char min = char.MinValue;
            char max = char.MaxValue;
            Console.WriteLine($"The range of char is {min} to {max}");
        }
    }
}

但我没有得到 U+0000 和 U+ffff 格式的输出。

如何获得?

【问题讨论】:

  • 您希望将它们打印为十六进制整数。
  • 是的十六进制,但前面有 U+

标签: c# unicode char


【解决方案1】:

您的问题是 char 在格式化为字符串时已经表示为字符。我的意思是具有 0x30 值的 char 表示为 0,而不是 48

因此,您需要将这些值转换为 int 并将它们显示为十六进制(使用格式说明符 X):

int min = char.MinValue; // int - not char
int max = char.MaxValue;
Console.WriteLine($"The range of char is U+{min:x4} to U+{max:x4}");

查看它们的数字(十六进制)值。

结果:

The range of char is U+0000 to U+ffff

【讨论】:

  • 它给了我想要的输出,但是否可以在不使用 U+{min} 的情况下获得 U+0000 的输出?
  • @puregeek 不,我不这么认为。 (afaik)没有内置方式可以说“打印此字符,因为它是 unicode 值”
  • 如果我以 unicode 值的形式输入,例如 U+0000,C# 是否可以识别为 char 还是必须将其输入为 0000?
猜你喜欢
  • 2015-02-20
  • 2020-09-19
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
相关资源
最近更新 更多