Convert a Unicode string to an escaped ASCII string

前端 未结 9 1516
广开言路
广开言路 2020-11-22 04:00

How can I convert this string:

This string contains the Unicode character Pi(π)

into an escaped A

9条回答
  •  时光取名叫无心
    2020-11-22 04:55

    To store actual Unicode codepoints, you have to first decode the String's UTF-16 codeunits to UTF-32 codeunits (which are currently the same as the Unicode codepoints). Use System.Text.Encoding.UTF32.GetBytes() for that, and then write the resulting bytes to the StringBuilder as needed,i.e.

    static void Main(string[] args) 
    { 
        String originalString = "This string contains the unicode character Pi(π)"; 
        Byte[] bytes = Encoding.UTF32.GetBytes(originalString);
        StringBuilder asAscii = new StringBuilder();
        for (int idx = 0; idx < bytes.Length; idx += 4)
        { 
            uint codepoint = BitConverter.ToUInt32(bytes, idx);
            if (codepoint <= 127) 
                asAscii.Append(Convert.ToChar(codepoint)); 
            else 
                asAscii.AppendFormat("\\u{0:x4}", codepoint); 
        } 
        Console.WriteLine("Final string: {0}", asAscii); 
        Console.ReadKey(); 
    }
    

提交回复
热议问题