在.Net中找出Unicode字符名称

svi*_*ick 22 .net unicode

在.Net中是否有办法找出某个字符具有哪些Unicode名称?

如果没有,是否有可以执行此操作的库?

Rik*_*ley 25

现在比以往任何时候都容易,因为nuget中有一个名为Unicode Information的包

有了这个,你可以打电话:

UnicodeInfo.GetName(character)
Run Code Online (Sandbox Code Playgroud)


Tri*_*nko 12

这是一个可以立即实现的解决方案,如复制/粘贴/编译.

首先,在此处下载Unicode数据库(UCD):http://www.unicode.org/Public/UNIDATA/UnicodeData.txt

接下来,将此代码添加到项目中以读取UCD并创建一个Dictionary以查找.NET char值的名称:

string[] unicodedata = File.ReadAllLines( "UnicodeData.txt", Encoding.UTF8 );
Dictionary<char,string> charname_map = new Dictionary<char,string>( 65536 );
for (int i = 0; i < unicodedata.Length; i++)
{
    string[] fields = unicodedata[i].Split( ';' );
    int char_code = int.Parse( fields[0], NumberStyles.HexNumber );
    string char_name = fields[1];
    if (char_code >= 0 && char_code <= 0xFFFF) //UTF-16 BMP code points only
    {
        bool is_range = char_name.EndsWith( ", First>" );
        if (is_range) //add all characters within a specified range
        {
            char_name = char_name.Replace( ", First", String.Empty ); //remove range indicator from name
            fields = unicodedata[++i].Split( ';' );
            int end_char_code = int.Parse( fields[0], NumberStyles.HexNumber );
            if (!fields[1].EndsWith( ", Last>" ))
                throw new Exception( "Expected end-of-range indicator." );
            for (int code_in_range = char_code; code_in_range <= end_char_code; code_in_range++)
                charname_map.Add( (char)code_in_range, char_name );
        }
        else
            charname_map.Add( (char)char_code, char_name );
    }
}
Run Code Online (Sandbox Code Playgroud)

UnicodeData.txt文件是UTF-8编码的,由每个Unicode代码点的一行信息组成.每行包含一个以分号分隔的字段列表,其中第一个字段是十六进制的Unicode代码点(没有前缀),第二个字段是字符名称.有关文件和每行包含的其他字段的信息可以在这里找到:有关UCD格式的信息可以在这里找到:http://www.unicode.org/reports/tr44/#Format_Conventions

使用上面的代码构建字符到字符名称的映射后,您只需从地图中检索它们,如下所示:

char c = 'Â';
string character_name;
if (!charname_map.TryGetValue( c, out character_name ))
    character_name = "<Character Name Missing>"; //character not found in map
//character_name should now contain "LATIN CAPITAL LETTER A WITH CIRCUMFLEX";
Run Code Online (Sandbox Code Playgroud)

我建议在应用程序资源中嵌入UnicodeData.txt文件,并将此代码包装到一个类中,该类在静态初始化程序中加载和解析文件一次.为了使代码更具可读性,您可以在类'char'类中实现扩展方法,如'GetUnicodeName'.我故意将值限制在0到0xFFFF的范围内,因为这是.NET UTF-16 char所能容纳的..NET char实际上并不代表真正的"字符"(也称为代码点),而是Unicode UTF-16代码单元,因为一些"字符"实际上需要两个代码单元.这样的一对代码单元被称为高和低代理.高于0xFFFF(16位字符可以存储的最大值)的值超出基本多语言平面(BMP),并且根据UTF-16编码需要两个chars进行编码.作为代理对的一部分的个别代码最终将具有诸如"非私人使用高代理人","私人使用高代理人"和"低代理人"之类的名称.


Tho*_*que 10

如果使用Process Monitor查看访问的文件charmap.exe,您将看到它打开一个名为的文件C:\Windows\system32\getuname.dll.此文件包含其资源中的字符名称(实际上资源本身位于特定于区域性的子目录中的.mui文件中).

所以你要做的就是使用LoadStringAPI 从这个文件中获取名称.我写了一个帮助类来做到这一点:

public class Win32ResourceReader : IDisposable
{
    private IntPtr _hModule;

    public Win32ResourceReader(string filename)
    {
        _hModule = LoadLibraryEx(filename, IntPtr.Zero, LoadLibraryFlags.AsDataFile | LoadLibraryFlags.AsImageResource);
        if (_hModule == IntPtr.Zero)
            throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
    }

    public string GetString(uint id)
    {
        var buffer = new StringBuilder(1024);
        LoadString(_hModule, id, buffer, buffer.Capacity);
        if (Marshal.GetLastWin32Error() != 0)
            throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
        return buffer.ToString();
    }

    ~Win32ResourceReader()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void Dispose(bool disposing)
    {
        if (_hModule != IntPtr.Zero)
            FreeLibrary(_hModule);
        _hModule = IntPtr.Zero;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool FreeLibrary(IntPtr hModule);

    [Flags]
    enum LoadLibraryFlags : uint
    {
        AsDataFile = 0x00000002,
        AsImageResource = 0x00000020
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

string path = @"C:\Windows\System32\getuname.dll";
using (var reader = new Win32ResourceReader(path))
{
    string name = reader.GetString(0xA9);
    Console.WriteLine(name); // Copyright Sign
}
Run Code Online (Sandbox Code Playgroud)


Han*_*ant 6

它不是.NET中的内置功能.您可以从Charmap.exe中找到它,它在状态栏中显示代码点名称.如果您在自己的程序中需要,可以将Unicode字符数据库编译到您的应用程序中.谨防版权.