나는 어제이 일을 정리해야했습니다.
단일 문자를 보는 것보다 조금 복잡합니다. 자신의 HtmlEncode() 메서드를 롤업해야합니다. .Net 세계의 문자열은 UTF-16으로 인코딩됩니다. 유니 코드 코드 포인트 (HTML 숫자 문자 참조가 식별하는 것)는 32 비트 부호없는 정수 값입니다. 이것은 주로 유니 코드 "기본 다국 언어"이외의 문자를 처리해야한다는 점에서 문제입니다.
이 코드는이 도움이
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 ;
}
/// <summary>
/// Convert a UTF-16 encoded .Net string into an array of UTF-32 encoding Unicode chars
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
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 ;
}
}
}
희망하고 싶은 일을해야한다!
어떤 점에서 그렇지 않은 같은? –