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

根据汉字得拼音的程序,在本机运行没问题,到服务器上报错
/// <summary>
  /// 把汉字转换成拼音(全拼)
  /// </summary>
  /// <param name="hzString">汉字字符串</param>
  /// <returns>转换后的拼音(全拼)字符串</returns>
  public static string Convert(string hzString)
  {
  // 匹配中文字符
  Regex regex = new Regex("^[\u4e00-\u9fa5]$");
  byte[] array = new byte[2];
  string pyString = "";
  int chrAsc = 0;
  int i1 = 0;
  int i2 = 0;
  char[] noWChar = hzString.ToCharArray();

  for (int j = 0; j < noWChar.Length; j++)
  {
  // 中文字符
  if (regex.IsMatch(noWChar[j].ToString()))
  {
  array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
  i1 = (short)(array[0]);
  i2 = (short)(array[1]);
  chrAsc = i1 * 256 + i2 - 65536;
  if (chrAsc > 0 && chrAsc < 160)
  {
  pyString += noWChar[j];
  }
  else
  {
  // 修正部分文字
  if (chrAsc == -9254) // 修正“圳”字
  pyString += "Zhen";
  else if (chrAsc == -3589) //修正覃
  pyString += "Qin";
  else if (chrAsc == -14637)//修正朴
  pyString += "Piao";

  else
  {
  for (int i = (pyValue.Length - 1); i >= 0; i--)
  {
  if (pyValue[i] <= chrAsc)
  {
  pyString += pyName[i];
  break;
  }
  }
  }
  }
  }
  // 非中文字符
  else
  {
  pyString += noWChar[j].ToString();
  }
  }
  return pyString;
  }




在本机运行好好的,但是放在服务器上出现



Index was outside the bounds of the array. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error: 


Line 122: array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
Line 123: i1 = (short)(array[0]);
Line 124: i2 = (short)(array[1]);
Line 125: chrAsc = i1 * 256 + i2 - 65536;
Line 126: if (chrAsc > 0 && chrAsc < 160)
 


------解决方案--------------------
这个应该和操作系统的语言版本有关,服务器上的是E文的吧?
------解决方案--------------------

array = System.Text.Encoding.GetEncoding("gb2312").GetBytes(noWChar[j].ToString());