如何将unicode字符转换为c#中的转义ascii等效字符

我开始使用包含编码的u​​nicode字符“ ü ”的字符串。 我将字符串传递给执行某些逻辑并返回另一个字符串的对象。 该字符串将原始编码字符转换为其unicode等效“ ü ”。

我需要恢复原始编码字符,但到目前为止还不能。

我已经尝试使用HttpUtility.HtmlEncode()方法但是返回“ ü ”这是不一样的。

有人可以帮忙吗?

它们几乎相同,至少用于显示目的。 HttpUtility.HtmlEncode使用十进制编码,格式为&#DECIMAL; 而您的原始版本采用hex编码,即格式为&#xHEX; 。 因为hex中的fc是十进制的252 ,所以两者是等价的。

如果你真的需要获得hex编码版本,那么考虑解析小数并将其转换为hex ,然后将其重新填入 &#xHEX; 格式。 就像是

 string unicode = "ü"; string decimalEncoded = HttpUtility.HtmlEncode(unicode); int decimal = int.Parse(decimalEncoded.Substring(2, decimalEncoded.Length - 3); string hexEncoded = string.Format("&#x{0:X};", decimal); 

我不得不把这个问题排除在外。

这比看单个角色要复杂一点。 您需要滚动自己的HtmlEncode()方法。 .Net世界中的字符串是UTF-16编码的。 Unicode代码点(HTML数字字符引用标识的内容)是32位无符号整数值。 这主要是一个问题,你必须处理Unicodes以外的人物“基本的多语言平面”。

这段代码应该做你想要的

 using System; using System.Configuration ; using System.Globalization ; using System.Collections.Generic ; using System.Text; namespace TestDrive { class Program { static void Main() { string src = "foo \uABC123 bar" ; string converted = HtmlEncode(src) ; return ; } static string HtmlEncode( string s ) { // // In the .Net world, strings are UTF-16 encoded. That means that Unicode codepoints greater than 0x007F // are encoded in the string as 2-character digraphs. So to properly turn them into HTML numeric // characeter references (decimal or hex), we first need to get the UTF-32 encoding. // uint[] utf32Chars = StringToArrayOfUtf32Chars( s ) ; StringBuilder sb = new StringBuilder( 2000 ) ; // set a reasonable initial size for the buffer // iterate over the utf-32 encoded characters foreach ( uint codePoint in utf32Chars ) { if ( codePoint > 0x0000007F ) { // if the code point is greater than 0x7F, it gets turned into an HTML numerica character reference sb.AppendFormat( "&#x{0:X};" , codePoint ) ; // hex escape sequence //sb.AppendFormat( "&#{0};" , codePoint ) ; // decimal escape sequence } else { // if less than or equal to 0x7F, it goes into the string as-is, // except for the 5 SGML/XML/HTML reserved characters. You might // want to also escape all the ASCII control characters (those chars // in the range 0x00 - 0x1F). // convert the unit to an UTF-16 character char ch = Convert.ToChar( codePoint ) ; // do the needful. switch ( ch ) { case '"' : sb.Append( """ ) ; break ; case '\'' : sb.Append( "'" ) ; break ; case '&' : sb.Append( "&" ) ; break ; case '<' : sb.Append( "<" ) ; break ; case '>' : sb.Append( ">" ) ; break ; default : sb.Append( ch.ToString() ) ; break ; } } } // return the escaped, utf-16 string back to the caller. string encoded = sb.ToString() ; return encoded ; } ///  /// Convert a UTF-16 encoded .Net string into an array of UTF-32 encoding Unicode chars ///  ///  ///  private static uint[] StringToArrayOfUtf32Chars( string s ) { Byte[] bytes = Encoding.UTF32.GetBytes( s ) ; uint[] utf32Chars = (uint[]) Array.CreateInstance( typeof(uint) , bytes.Length / sizeof(uint) ) ; for ( int i = 0 , j = 0 ; i < bytes.Length ; i += 4 , ++j ) { utf32Chars[ j ] = BitConverter.ToUInt32( bytes , i ) ; } return utf32Chars ; } } } 

希望这可以帮助!

或者您可以尝试以下代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Configuration; using System.Globalization; namespace SimpleCGIEXE { class Program { static string Uni2Html(string src) { string temp1 = HttpUtility.UrlEncodeUnicode(src); string temp2 = temp1.Replace('+', ' '); string res = string.Empty; int pos1 = 0, pos2 = 0; while (true){ pos2=temp2.IndexOf("%",pos1); if (pos2 < 0) break; if (temp2[pos2 + 1] == 'u') { res += temp2.Substring(pos1, pos2 - pos1); res += "&#x"; res += temp2.Substring(pos2 + 2, 4); res += ";"; pos1 = pos2 + 6; } else { res += temp2.Substring(pos1, pos2 - pos1); string stASCII = temp2.Substring(pos2 + 1, 2); byte[] pdASCII = new byte[1]; pdASCII[0] = byte.Parse(stASCII, System.Globalization.NumberStyles.AllowHexSpecifier); res += Encoding.ASCII.GetString(pdASCII); pos1 = pos2 + 3; } } res += temp2.Substring(pos1); return res; } static void Main(string[] args) { Console.WriteLine("Content-type: text/html;charset=utf-8\r\n"); String st = "Vietnamese string: Thử một xâu unicode @@ # ~ .^ % !"; Console.WriteLine(Uni2Html(st) + "
"); st = "A chinese string: 我爱你 (I love you)"; Console.WriteLine(Uni2Html(st) + "
"); } } }