日期:2014-05-18  浏览次数:20976 次

辛苦了半天写的获取汉字笔画数的代码(C#)居然不行?
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {

  string cnWords = "汉";
  string cnWords1 = GetUTF8(cnWords);
  string cn1 = cnWords1.Substring(0, 2);
  string cn2 = cnWords1.Substring(2, 2);

  //cn1 = "B2";
  //cn2 = "B2";
   
  Response.Write(cnWords + "=");
  Response.Write(cn1);
  Response.Write(cn2);

  int cn11 = int.Parse(cn1, System.Globalization.NumberStyles.HexNumber);
  int cn22 = int.Parse(cn2, System.Globalization.NumberStyles.HexNumber);

  Response.Write("</br>"+"十进制是");
  Response.Write(cn11);
  Response.Write(cn22);

  Response.Write("</br>");
  Response.Write(cnWords + "的笔画是:");
  Response.Write(GetGB2312StrokeCount.GetGB2312StrokeCountM(cn11 ,cn22));
  //Response.Write("</br>");
   
   
  }

   
public string GetUTF8(string my)
  {
   
  byte[] bytes = System.Text.Encoding.Unicode.GetBytes(my);
  string lowCode = "", temp = "";
  for (int i = 0; i < bytes.Length; i++)
  {
  if (i % 2 == 0)
  {
  temp = System.Convert.ToString(bytes[i], 16);//取出元素4编码内容(两位16进制)
  if (temp.Length < 2)
  temp = "0" + temp;

  }
  else
  {
  string mytemp = System.Convert.ToString(bytes[i], 16);
  if (mytemp.Length < 2) mytemp = "0" + mytemp;
  lowCode = lowCode + mytemp + temp;//取出元素4编码内容(两位16进制)

  }
  }
  return lowCode;
  } 
}


------解决方案--------------------
一个是UTF-8...一个是GB2312...能一样吗?

转编码试试...
------解决方案--------------------
Unicode是字符集,不是编码。
编码是编码,不过一般一个字符集之对应一种编码,所以容易混淆,但Unicode有很多种编码方式。