In C# how to get minimum and maximum value of char printed in Unicode format?

廉价感情. 提交于 2021-02-08 06:14:33

问题


According to MSDN the minimum value of char is U+0000 and maximum value of char is U+ffff

I have written the following code to print the same:

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

But I am not getting the output in the format U+0000 and U+ffff.

How to get it?


回答1:


Your problem is that char when formatted into a string is already represented as the character. I mean the char with value 0x30 is represented as 0, not as 48.

So you need to cast those values as int and display them hexadecimal (using the format specifier 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}");

to see their numerical (hexadecimal) value.

Result:

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


来源:https://stackoverflow.com/questions/47892186/in-c-sharp-how-to-get-minimum-and-maximum-value-of-char-printed-in-unicode-forma

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!