.
, . HtmlEncode(). .Net UTF-16. Unicode codepoints (, 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 )
{
uint[] utf32Chars = StringToArrayOfUtf32Chars( s ) ;
StringBuilder sb = new StringBuilder( 2000 ) ;
foreach ( uint codePoint in utf32Chars )
{
if ( codePoint > 0x0000007F )
{
sb.AppendFormat( "&#x{0:X};" , codePoint ) ;
}
else
{
char ch = Convert.ToChar( codePoint ) ;
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 ;
}
}
}
string encoded = sb.ToString() ;
return encoded ;
}
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 ;
}
}
}
, !