天天看點

漢字轉化為拼音

很多問題都是因為中文問題造成的

如檔案名最好别用中文

現在的解決方法一般是産生一個ID,将這個ID做檔案名

網頁上如果url帶漢字也經常出錯

現在的解決方法一般用urlencode編碼

現在用了這個轉化,就好多了

原理:利用哈希表技術   Hashtable ht=new Hashtable(10);

1.添加索引,如: ht.put("-20295","ang");

2.倒序周遊,如:  while(!ht.containsKey(Integer.toString(num)))num--;

<%@ page language="java" import="java.util.*" contentType="text/html" %>

<%!

Hashtable ht=new Hashtable(10);

String g(Integer n){

 int num=n.intValue();

 if(num>0&&num<160){

  return String.valueOf((char)num);

 }

 else if(num<-20319||num>-10247){

  return "";

 }

 else{

  if(ht.size()==0){b();}

  while(!ht.containsKey(Integer.toString(num)))num--;

  return ht.get(Integer.toString(num)).toString();

 }

}

String c(String str){

 char[] hz=str.toCharArray();

 int len=str.length();

 int p,q;

 String ret="";

 for(int i=0;i<len;i++){

  p=(int)hz[i];

  if(p>160){

   q=(int)hz[++i];

   p=p*256+q-65536;

  }

  ret+=g(new Integer(p));

 }

 return ret;

}

void b(){

 ht.put("-20319","a");

 ht.put("-20317","ai");

 ht.put("-20304","an");

 ht.put("-20295","ang");

 ht.put("-20292","ao");

 ht.put("-20283","ba");

 ht.put("-20265","bai");

 ht.put("-20257","ban");

 ht.put("-20242","bang");

 ht.put("-20230","bao");

 ht.put("-20051","bei");

 ht.put("-20036","ben");

 ht.put("-20032","beng");

 ht.put("-20026","bi");

 ht.put("-20002","bian");

 ht.put("-19990","biao");

 ht.put("-19986","bie");

 ht.put("-19982","bin");

 ht.put("-19976","bing");

 ht.put("-19805","bo");

 ht.put("-19784","bu");

 ht.put("-19775","ca");

 ht.put("-19774","cai");

 ht.put("-19763","can");

 ht.put("-19756","cang");

 ht.put("-19751","cao");

 ht.put("-19746","ce");

 ht.put("-19741","ceng");

 ht.put("-19739","cha");

 ht.put("-19728","chai");

 ht.put("-19725","chan");

 ht.put("-19715","chang");

 ht.put("-19540","chao");

 ht.put("-19531","che");

 ht.put("-19525","chen");

 ht.put("-19515","cheng");

 ht.put("-19500","chi");

 ht.put("-19484","chong");

 ht.put("-19479","chou");

 ht.put("-19467","chu");

 ht.put("-19289","chuai");

 ht.put("-19288","chuan");

 ht.put("-19281","chuang");

 ht.put("-19275","chui");

 ht.put("-19270","chun");

 ht.put("-19263","chuo");

 ht.put("-19261","ci");

 ht.put("-19249","cong");

 ht.put("-19243","cou");

 ht.put("-19242","cu");

 ht.put("-19238","cuan");

 ht.put("-19235","cui");

 ht.put("-19227","cun");

 ht.put("-19224","cuo");

 ht.put("-19218","da");

 ht.put("-19212","dai");

 ht.put("-19038","dan");

 ht.put("-19023","dang");

 ht.put("-19018","dao");

 ht.put("-19006","de");

 ht.put("-19003","deng");

 ht.put("-18996","di");

 ht.put("-18977","dian");

 ht.put("-18961","diao");

 ht.put("-18952","die");

 ht.put("-18783","ding");

 ht.put("-18774","diu");

 ht.put("-18773","dong");

 ht.put("-18763","dou");

 ht.put("-18756","du");

 ht.put("-18741","duan");

 ht.put("-18735","dui");

 ht.put("-18731","dun");

 ht.put("-18722","duo");

 ht.put("-18710","e");

 ht.put("-18697","en");

 ht.put("-18696","er");

 ht.put("-18526","fa");

 ht.put("-18518","fan");

 ht.put("-18501","fang");

 ht.put("-18490","fei");

 ht.put("-18478","fen");

 ht.put("-18463","feng");

 ht.put("-18448","fo");

 ht.put("-18447","fou");

 ht.put("-18446","fu");

 ht.put("-18239","ga");

 ht.put("-18237","gai");

 ht.put("-18231","gan");

 ht.put("-18220","gang");

 ht.put("-18211","gao");

 ht.put("-18201","ge");

 ht.put("-18184","gei");

 ht.put("-18183","gen");

 ht.put("-18181","geng");

 ht.put("-18012","gong");

 ht.put("-17997","gou");

 ht.put("-17988","gu");

 ht.put("-17970","gua");

 ht.put("-17964","guai");

 ht.put("-17961","guan");

 ht.put("-17950","guang");

 ht.put("-17947","gui");

 ht.put("-17931","gun");

 ht.put("-17928","guo");

 ht.put("-17922","ha");

 ht.put("-17759","hai");

 ht.put("-17752","han");

 ht.put("-17733","hang");

 ht.put("-17730","hao");

 ht.put("-17721","he");

 ht.put("-17703","hei");

 ht.put("-17701","hen");

 ht.put("-17697","heng");

 ht.put("-17692","hong");

 ht.put("-17683","hou");

 ht.put("-17676","hu");

 ht.put("-17496","hua");

 ht.put("-17487","huai");

 ht.put("-17482","huan");

 ht.put("-17468","huang");

 ht.put("-17454","hui");

 ht.put("-17433","hun");

 ht.put("-17427","huo");

 ht.put("-17417","ji");

 ht.put("-17202","jia");

 ht.put("-17185","jian");

 ht.put("-16983","jiang");

 ht.put("-16970","jiao");

 ht.put("-16942","jie");

 ht.put("-16915","jin");

 ht.put("-16733","jing");

 ht.put("-16708","jiong");

 ht.put("-16706","jiu");

 ht.put("-16689","ju");

 ht.put("-16664","juan");

 ht.put("-16657","jue");

 ht.put("-16647","jun");

 ht.put("-16474","ka");

 ht.put("-16470","kai");

 ht.put("-16465","kan");

 ht.put("-16459","kang");

 ht.put("-16452","kao");

 ht.put("-16448","ke");

 ht.put("-16433","ken");

 ht.put("-16429","keng");

 ht.put("-16427","kong");

 ht.put("-16423","kou");

 ht.put("-16419","ku");

 ht.put("-16412","kua");

 ht.put("-16407","kuai");

 ht.put("-16403","kuan");

 ht.put("-16401","kuang");

 ht.put("-16393","kui");

 ht.put("-16220","kun");

 ht.put("-16216","kuo");

 ht.put("-16212","la");

 ht.put("-16205","lai");

 ht.put("-16202","lan");

 ht.put("-16187","lang");

 ht.put("-16180","lao");

 ht.put("-16171","le");

 ht.put("-16169","lei");

 ht.put("-16158","leng");

 ht.put("-16155","li");

 ht.put("-15959","lia");

 ht.put("-15958","lian");

 ht.put("-15944","liang");

 ht.put("-15933","liao");

 ht.put("-15920","lie");

 ht.put("-15915","lin");

 ht.put("-15903","ling");

 ht.put("-15889","liu");

 ht.put("-15878","long");

 ht.put("-15707","lou");

 ht.put("-15701","lu");

 ht.put("-15681","lv");

 ht.put("-15667","luan");

 ht.put("-15661","lue");

 ht.put("-15659","lun");

 ht.put("-15652","luo");

 ht.put("-15640","ma");

 ht.put("-15631","mai");

 ht.put("-15625","man");

 ht.put("-15454","mang");

 ht.put("-15448","mao");

 ht.put("-15436","me");

 ht.put("-15435","mei");

 ht.put("-15419","men");

 ht.put("-15416","meng");

 ht.put("-15408","mi");

 ht.put("-15394","mian");

 ht.put("-15385","miao");

 ht.put("-15377","mie");

 ht.put("-15375","min");

 ht.put("-15369","ming");

 ht.put("-15363","miu");

 ht.put("-15362","mo");

 ht.put("-15183","mou");

 ht.put("-15180","mu");

 ht.put("-15165","na");

 ht.put("-15158","nai");

 ht.put("-15153","nan");

 ht.put("-15150","nang");

 ht.put("-15149","nao");

 ht.put("-15144","ne");

 ht.put("-15143","nei");

 ht.put("-15141","nen");

 ht.put("-15140","neng");

 ht.put("-15139","ni");

 ht.put("-15128","nian");

 ht.put("-15121","niang");

 ht.put("-15119","niao");

 ht.put("-15117","nie");

 ht.put("-15110","nin");

 ht.put("-15109","ning");

 ht.put("-14941","niu");

 ht.put("-14937","nong");

 ht.put("-14933","nu");

 ht.put("-14930","nv");

 ht.put("-14929","nuan");

 ht.put("-14928","nue");

 ht.put("-14926","nuo");

 ht.put("-14922","o");

 ht.put("-14921","ou");

 ht.put("-14914","pa");

 ht.put("-14908","pai");

 ht.put("-14902","pan");

 ht.put("-14894","pang");

 ht.put("-14889","pao");

 ht.put("-14882","pei");

 ht.put("-14873","pen");

 ht.put("-14871","peng");

 ht.put("-14857","pi");

 ht.put("-14678","pian");

 ht.put("-14674","piao");

 ht.put("-14670","pie");

 ht.put("-14668","pin");

 ht.put("-14663","ping");

 ht.put("-14654","po");

 ht.put("-14645","pu");

 ht.put("-14630","qi");

 ht.put("-14594","qia");

 ht.put("-14429","qian");

 ht.put("-14407","qiang");

 ht.put("-14399","qiao");

 ht.put("-14384","qie");

 ht.put("-14379","qin");

 ht.put("-14368","qing");

 ht.put("-14355","qiong");

 ht.put("-14353","qiu");

 ht.put("-14345","qu");

 ht.put("-14170","quan");

 ht.put("-14159","que");

 ht.put("-14151","qun");

 ht.put("-14149","ran");

 ht.put("-14145","rang");

 ht.put("-14140","rao");

 ht.put("-14137","re");

 ht.put("-14135","ren");

 ht.put("-14125","reng");

 ht.put("-14123","ri");

 ht.put("-14122","rong");

 ht.put("-14112","rou");

 ht.put("-14109","ru");

 ht.put("-14099","ruan");

 ht.put("-14097","rui");

 ht.put("-14094","run");

 ht.put("-14092","ruo");

 ht.put("-14090","sa");

 ht.put("-14087","sai");

 ht.put("-14083","san");

 ht.put("-13917","sang");

 ht.put("-13914","sao");

 ht.put("-13910","se");

 ht.put("-13907","sen");

 ht.put("-13906","seng");

 ht.put("-13905","sha");

 ht.put("-13896","shai");

 ht.put("-13894","shan");

 ht.put("-13878","shang");

 ht.put("-13870","shao");

 ht.put("-13859","she");

 ht.put("-13847","shen");

 ht.put("-13831","sheng");

 ht.put("-13658","shi");

 ht.put("-13611","shou");

 ht.put("-13601","shu");

 ht.put("-13406","shua");

 ht.put("-13404","shuai");

 ht.put("-13400","shuan");

 ht.put("-13398","shuang");

 ht.put("-13395","shui");

 ht.put("-13391","shun");

 ht.put("-13387","shuo");

 ht.put("-13383","si");

 ht.put("-13367","song");

 ht.put("-13359","sou");

 ht.put("-13356","su");

 ht.put("-13343","suan");

 ht.put("-13340","sui");

 ht.put("-13329","sun");

 ht.put("-13326","suo");

 ht.put("-13318","ta");

 ht.put("-13147","tai");

 ht.put("-13138","tan");

 ht.put("-13120","tang");

 ht.put("-13107","tao");

 ht.put("-13096","te");

 ht.put("-13095","teng");

 ht.put("-13091","ti");

 ht.put("-13076","tian");

 ht.put("-13068","tiao");

 ht.put("-13063","tie");

 ht.put("-13060","ting");

 ht.put("-12888","tong");

 ht.put("-12875","tou");

 ht.put("-12871","tu");

 ht.put("-12860","tuan");

 ht.put("-12858","tui");

 ht.put("-12852","tun");

 ht.put("-12849","tuo");

 ht.put("-12838","wa");

 ht.put("-12831","wai");

 ht.put("-12829","wan");

 ht.put("-12812","wang");

 ht.put("-12802","wei");

 ht.put("-12607","wen");

 ht.put("-12597","weng");

 ht.put("-12594","wo");

 ht.put("-12585","wu");

 ht.put("-12556","xi");

 ht.put("-12359","xia");

 ht.put("-12346","xian");

 ht.put("-12320","xiang");

 ht.put("-12300","xiao");

 ht.put("-12120","xie");

 ht.put("-12099","xin");

 ht.put("-12089","xing");

 ht.put("-12074","xiong");

 ht.put("-12067","xiu");

 ht.put("-12058","xu");

 ht.put("-12039","xuan");

 ht.put("-11867","xue");

 ht.put("-11861","xun");

 ht.put("-11847","ya");

 ht.put("-11831","yan");

 ht.put("-11798","yang");

 ht.put("-11781","yao");

 ht.put("-11604","ye");

 ht.put("-11589","yi");

 ht.put("-11536","yin");

 ht.put("-11358","ying");

 ht.put("-11340","yo");

 ht.put("-11339","yong");

 ht.put("-11324","you");

 ht.put("-11303","yu");

 ht.put("-11097","yuan");

 ht.put("-11077","yue");

 ht.put("-11067","yun");

 ht.put("-11055","za");

 ht.put("-11052","zai");

 ht.put("-11045","zan");

 ht.put("-11041","zang");

 ht.put("-11038","zao");

 ht.put("-11024","ze");

 ht.put("-11020","zei");

 ht.put("-11019","zen");

 ht.put("-11018","zeng");

 ht.put("-11014","zha");

 ht.put("-10838","zhai");

 ht.put("-10832","zhan");

 ht.put("-10815","zhang");

 ht.put("-10800","zhao");

 ht.put("-10790","zhe");

 ht.put("-10780","zhen");

 ht.put("-10764","zheng");

 ht.put("-10587","zhi");

 ht.put("-10544","zhong");

 ht.put("-10533","zhou");

 ht.put("-10519","zhu");

 ht.put("-10331","zhua");

 ht.put("-10329","zhuai");

 ht.put("-10328","zhuan");

 ht.put("-10322","zhuang");

 ht.put("-10315","zhui");

 ht.put("-10309","zhun");

 ht.put("-10307","zhuo");

 ht.put("-10296","zi");

 ht.put("-10281","zong");

 ht.put("-10274","zou");

 ht.put("-10270","zu");

 ht.put("-10262","zuan");

 ht.put("-10260","zui");

 ht.put("-10256","zun");

 ht.put("-10254","zuo");

 ht.put("-10247","zz");

}

%>

<%

if(request.getParameter("hz")!=null)

{

 out.println(c(request.getParameter("hz")));

}

%>

<form method=post>

請在此處輸入中文:<input name=hz>

</form>

用C#實作漢字轉化為拼音

其實作的原理就是先将漢字轉化成為内碼,然後通過内碼和拼音的對照來查找。

詳細的代碼如下所示:

public class chs2py

 {/// <summary>

 ///将漢字轉換成為拼音

 ///作者:鐵匠(chinahuman)

 ///完成于:2004年3月12日早上

 /// </summary>

  private static int[] pyvalue=new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,

    -20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,

    -19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,

    -19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,

    -18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526,

    -18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,

    -18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,

    -17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,

    -17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,

    -16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,

    -16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,

    -15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,

    -15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,

    -15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,

    -14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,

    -14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,

    -14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,

    -14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,

    -13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,

    -13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,

    -12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,

    -12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,

    -11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,

    -11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,

    -10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254};

  private static string[] pystr=new string[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",

   "bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",

   "cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",

   "cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",

   "dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",

   "ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",

   "hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",

   "jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",

   "keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",

   "leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",

   "ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",

   "na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",

   "nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",

   "pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",

   "ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",

   "sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",

   "shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",

   "tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",

   "wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",

   "xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",

   "yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",

   "zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",

   "zi","zong","zou","zu","zuan","zui","zun","zuo"};

  public chs2py()

  {

   //

   // TODO: 在此處添加構造函數邏輯

   //

  }

  public static string convert(string chrstr)

  {

   byte[] array = new byte[2];

   string returnstr="";

   int chrasc=0;

   int i1 = 0;

   int i2 =0;

   char[] nowchar=chrstr.ToCharArray();

   for (int j=0;j<nowchar.Length ;j++)

   {

     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)

    {

     returnstr+=nowchar[j];

    }

    else

    {

     for (int i=(pyvalue.Length -1);i>=0;i--)

     {

      if (pyvalue[i]<=chrasc)

      {

       returnstr+=pystr[i];

       break;

      }

     }

    }

   }

   return returnstr;

  }

 }