繼上一篇 如何在ASP內判定BIG5碼中文字 之後, 再來整理一下在 ASP.NET C# 中判定 BIG5 碼的方式, 其實方式一樣很單純, 不過不需要利用到轉碼的 code page, 只需要利用 System.Text.Encoding 即可順利判讀, 程式碼如下:
Boolean IsBig5(string strOne)
{
Boolean bIsBig5 = false;
byte[] ByteArrayBig5 = System.Text.Encoding.GetEncoding("big5").GetBytes(strOne);
if (ByteArrayBig5.Length == 2)
{
//check A440~C67E 常用字, C940~F9D5 次常用字
//42048~50814, 51520~63957
int iCode = ByteArrayBig5[0] * 256 + ByteArrayBig5[1];
if ((iCode >= 42048 && iCode <= 50814) || (iCode >= 51520 && iCode <= 63957))
{
// chinese char big5
bIsBig5 = true;
}
}
return bIsBig5;
}
利用了 Encoding 的 GetBytes 方式, 取出在 strOne 中的一個字元, 並判定是否為 2個 byte 後, 一樣利用了原來的判定方式, 若落在對應的區間內, 就可以判定為 BIG5 字元集內的字了!