简体   繁体   English

Unicode中的C#字符

[英]C# Character from Unicode

I have a unicode character from the FontAwesome cheat sheet: 我有一个FontAwesome速查表中的Unicode字符:

#xf042;

How do I put that character into c# ? 如何将该字符放入c#中?

string s = "????"; 字符串s =“ ????”;

I have tried entering it is as and using a . 我尝试输入as并使用。

Assuming the hex input represents UTF8 encoded string you could have a function that will convert a HEX string: 假设十六进制输入代表UTF8编码的字符串,则可以使用一个函数来转换十六进制字符串:

public static string ConvertHexToString(string hex)
{
    int numberChars = hex.Length;
    byte[] bytes = new byte[numberChars / 2];
    for (int i = 0; i < numberChars; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    }
    return Encoding.UTF8.GetString(bytes);
}

and then filter out the unnecessary characters from your input before feeding it to this function: 然后从输入中过滤掉不必要的字符,然后再将其提供给此功能:

string input = "#xf042;";
string s = input.Replace("#x", string.Empty).Replace(";", string.Empty);
string result = ConvertHexToString(s);

Obviously you will need to adjust the correct encoding based on the input, because the hex simply represents a byte array and in order to decode this byte array back to a string you're gonna need to know the encoding. 显然,您将需要根据输入来调整正确的编码,因为十六进制仅表示一个字节数组,并且为了将该字节数组解码回字符串,您需要了解编码。

If you just want a lighter version of what Darin posted to convert the hex value to a string containing the unicode position from the private area of the FontAwesome font, you can use this >> 如果您只想使用Darin发布的内容的较浅版本,以将十六进制值转换为包含FontAwesome字体的私有区域中的unicode位置的字符串,则可以使用此>>

private static string UnicodeToChar( string hex ) {
    int code=int.Parse( hex, System.Globalization.NumberStyles.HexNumber );
    string unicodeString=char.ConvertFromUtf32( code );
    return unicodeString;
}

Just call it as follows >> 只需如下称呼>>

string s = UnicodeToChar( "f042" );

Alternatively, you can simply use the C# class with all the icons and loader pre-written here >> FontAwesome For WinForms CSharp 或者,您可以简单地将C#类与所有图标和加载器一起使用在此处>> >> WinForms CSharp的FontAwesome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM