java程序将汉字转成拼音的两种方法

来源:互联网 发布:mac python numpy安装 编辑:程序博客网 时间:2024/05/21 10:14
1.使用pinyin4j将汉字转成拼音,附件为pinyin4j的jar包 
Java代码  收藏代码
  1. import net.sourceforge.pinyin4j.PinyinHelper;  
  2. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
  6. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
  7.   
  8. public class ChineseToEnglish {  
  9.     // 将汉字转换为全拼  
  10.     public static String getPingYin(String src) {  
  11.   
  12.         char[] t1 = null;  
  13.         t1 = src.toCharArray();  
  14.         String[] t2 = new String[t1.length];  
  15.         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
  16.           
  17.         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  18.         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  19.         t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
  20.         String t4 = "";  
  21.         int t0 = t1.length;  
  22.         try {  
  23.             for (int i = 0; i < t0; i++) {  
  24.                 // 判断是否为汉字字符  
  25.                 if (java.lang.Character.toString(t1[i]).matches(  
  26.                         "[\\u4E00-\\u9FA5]+")) {  
  27.                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);  
  28.                     t4 += t2[0];  
  29.                 } else  
  30.                     t4 += java.lang.Character.toString(t1[i]);  
  31.             }  
  32.             // System.out.println(t4);  
  33.             return t4;  
  34.         } catch (BadHanyuPinyinOutputFormatCombination e1) {  
  35.             e1.printStackTrace();  
  36.         }  
  37.         return t4;  
  38.     }  
  39.   
  40.     // 返回中文的首字母  
  41.     public static String getPinYinHeadChar(String str) {  
  42.   
  43.         String convert = "";  
  44.         for (int j = 0; j < str.length(); j++) {  
  45.             char word = str.charAt(j);  
  46.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
  47.             if (pinyinArray != null) {  
  48.                 convert += pinyinArray[0].charAt(0);  
  49.             } else {  
  50.                 convert += word;  
  51.             }  
  52.         }  
  53.         return convert;  
  54.     }  
  55.   
  56.     // 将字符串转移为ASCII码  
  57.     public static String getCnASCII(String cnStr) {  
  58.         StringBuffer strBuf = new StringBuffer();  
  59.         byte[] bGBK = cnStr.getBytes();  
  60.         for (int i = 0; i < bGBK.length; i++) {  
  61.             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));  
  62.         }  
  63.         return strBuf.toString();  
  64.     }  
  65.   
  66.     public static void main(String[] args) {  
  67.         System.out.println(getPingYin("綦江qq县"));  
  68.         System.out.println(getPinYinHeadChar("綦江县"));  
  69.         System.out.println(getCnASCII("綦江县"));  
  70.     }  
  71. }  


2.java程序直接转 
Java代码  收藏代码
  1. import java.util.Iterator;  
  2. import java.util.LinkedHashMap;  
  3. import java.util.Set;  
  4.   
  5. public class ChineseToEnglish2 {  
  6.     private static LinkedHashMap spellMap = null;  
  7.   
  8.     static {  
  9.         if (spellMap == null) {  
  10.             spellMap = new LinkedHashMap(500);  
  11.         }  
  12.         initialize();  
  13.     }  
  14.   
  15.     private ChineseToEnglish2() {  
  16.   
  17.     }  
  18.   
  19.     private static void spellPut(String spell, int ascii) {  
  20.         spellMap.put(spell, new Integer(ascii));  
  21.     }  
  22.   
  23.     private static void initialize() {  
  24.         spellPut("a", -20319);  
  25.         spellPut("ai", -20317);  
  26.         spellPut("an", -20304);  
  27.         spellPut("ang", -20295);  
  28.         spellPut("ao", -20292);  
  29.         spellPut("ba", -20283);  
  30.         spellPut("bai", -20265);  
  31.         spellPut("ban", -20257);  
  32.         spellPut("bang", -20242);  
  33.         spellPut("bao", -20230);  
  34.         spellPut("bei", -20051);  
  35.         spellPut("ben", -20036);  
  36.         spellPut("beng", -20032);  
  37.         spellPut("bi", -20026);  
  38.         spellPut("bian", -20002);  
  39.         spellPut("biao", -19990);  
  40.         spellPut("bie", -19986);  
  41.         spellPut("bin", -19982);  
  42.         spellPut("bing", -19976);  
  43.         spellPut("bo", -19805);  
  44.         spellPut("bu", -19784);  
  45.         spellPut("ca", -19775);  
  46.         spellPut("cai", -19774);  
  47.         spellPut("can", -19763);  
  48.         spellPut("cang", -19756);  
  49.         spellPut("cao", -19751);  
  50.         spellPut("ce", -19746);  
  51.         spellPut("ceng", -19741);  
  52.         spellPut("cha", -19739);  
  53.         spellPut("chai", -19728);  
  54.         spellPut("chan", -19725);  
  55.         spellPut("chang", -19715);  
  56.         spellPut("chao", -19540);  
  57.         spellPut("che", -19531);  
  58.         spellPut("chen", -19525);  
  59.         spellPut("cheng", -19515);  
  60.         spellPut("chi", -19500);  
  61.         spellPut("chong", -19484);  
  62.         spellPut("chou", -19479);  
  63.         spellPut("chu", -19467);  
  64.         spellPut("chuai", -19289);  
  65.         spellPut("chuan", -19288);  
  66.         spellPut("chuang", -19281);  
  67.         spellPut("chui", -19275);  
  68.         spellPut("chun", -19270);  
  69.         spellPut("chuo", -19263);  
  70.         spellPut("ci", -19261);  
  71.         spellPut("cong", -19249);  
  72.         spellPut("cou", -19243);  
  73.         spellPut("cu", -19242);  
  74.         spellPut("cuan", -19238);  
  75.         spellPut("cui", -19235);  
  76.         spellPut("cun", -19227);  
  77.         spellPut("cuo", -19224);  
  78.         spellPut("da", -19218);  
  79.         spellPut("dai", -19212);  
  80.         spellPut("dan", -19038);  
  81.         spellPut("dang", -19023);  
  82.         spellPut("dao", -19018);  
  83.         spellPut("de", -19006);  
  84.         spellPut("deng", -19003);  
  85.         spellPut("di", -18996);  
  86.         spellPut("dian", -18977);  
  87.         spellPut("diao", -18961);  
  88.         spellPut("die", -18952);  
  89.         spellPut("ding", -18783);  
  90.         spellPut("diu", -18774);  
  91.         spellPut("dong", -18773);  
  92.         spellPut("dou", -18763);  
  93.         spellPut("du", -18756);  
  94.         spellPut("duan", -18741);  
  95.         spellPut("dui", -18735);  
  96.         spellPut("dun", -18731);  
  97.         spellPut("duo", -18722);  
  98.         spellPut("e", -18710);  
  99.         spellPut("en", -18697);  
  100.         spellPut("er", -18696);  
  101.         spellPut("fa", -18526);  
  102.         spellPut("fan", -18518);  
  103.         spellPut("fang", -18501);  
  104.         spellPut("fei", -18490);  
  105.         spellPut("fen", -18478);  
  106.         spellPut("feng", -18463);  
  107.         spellPut("fo", -18448);  
  108.         spellPut("fou", -18447);  
  109.         spellPut("fu", -18446);  
  110.         spellPut("ga", -18239);  
  111.         spellPut("gai", -18237);  
  112.         spellPut("gan", -18231);  
  113.         spellPut("gang", -18220);  
  114.         spellPut("gao", -18211);  
  115.         spellPut("ge", -18201);  
  116.         spellPut("gei", -18184);  
  117.         spellPut("gen", -18183);  
  118.         spellPut("geng", -18181);  
  119.         spellPut("gong", -18012);  
  120.         spellPut("gou", -17997);  
  121.         spellPut("gu", -17988);  
  122.         spellPut("gua", -17970);  
  123.         spellPut("guai", -17964);  
  124.         spellPut("guan", -17961);  
  125.         spellPut("guang", -17950);  
  126.         spellPut("gui", -17947);  
  127.         spellPut("gun", -17931);  
  128.         spellPut("guo", -17928);  
  129.         spellPut("ha", -17922);  
  130.         spellPut("hai", -17759);  
  131.         spellPut("han", -17752);  
  132.         spellPut("hang", -17733);  
  133.         spellPut("hao", -17730);  
  134.         spellPut("he", -17721);  
  135.         spellPut("hei", -17703);  
  136.         spellPut("hen", -17701);  
  137.         spellPut("heng", -17697);  
  138.         spellPut("hong", -17692);  
  139.         spellPut("hou", -17683);  
  140.         spellPut("hu", -17676);  
  141.         spellPut("hua", -17496);  
  142.         spellPut("huai", -17487);  
  143.         spellPut("huan", -17482);  
  144.         spellPut("huang", -17468);  
  145.         spellPut("hui", -17454);  
  146.         spellPut("hun", -17433);  
  147.         spellPut("huo", -17427);  
  148.         spellPut("ji", -17417);  
  149.         spellPut("jia", -17202);  
  150.         spellPut("jian", -17185);  
  151.         spellPut("jiang", -16983);  
  152.         spellPut("jiao", -16970);  
  153.         spellPut("jie", -16942);  
  154.         spellPut("jin", -16915);  
  155.         spellPut("jing", -16733);  
  156.         spellPut("jiong", -16708);  
  157.         spellPut("jiu", -16706);  
  158.         spellPut("ju", -16689);  
  159.         spellPut("juan", -16664);  
  160.         spellPut("jue", -16657);  
  161.         spellPut("jun", -16647);  
  162.         spellPut("ka", -16474);  
  163.         spellPut("kai", -16470);  
  164.         spellPut("kan", -16465);  
  165.         spellPut("kang", -16459);  
  166.         spellPut("kao", -16452);  
  167.         spellPut("ke", -16448);  
  168.         spellPut("ken", -16433);  
  169.         spellPut("keng", -16429);  
  170.         spellPut("kong", -16427);  
  171.         spellPut("kou", -16423);  
  172.         spellPut("ku", -16419);  
  173.         spellPut("kua", -16412);  
  174.         spellPut("kuai", -16407);  
  175.         spellPut("kuan", -16403);  
  176.         spellPut("kuang", -16401);  
  177.         spellPut("kui", -16393);  
  178.         spellPut("kun", -16220);  
  179.         spellPut("kuo", -16216);  
  180.         spellPut("la", -16212);  
  181.         spellPut("lai", -16205);  
  182.         spellPut("lan", -16202);  
  183.         spellPut("lang", -16187);  
  184.         spellPut("lao", -16180);  
  185.         spellPut("le", -16171);  
  186.         spellPut("lei", -16169);  
  187.         spellPut("leng", -16158);  
  188.         spellPut("li", -16155);  
  189.         spellPut("lia", -15959);  
  190.         spellPut("lian", -15958);  
  191.         spellPut("liang", -15944);  
  192.         spellPut("liao", -15933);  
  193.         spellPut("lie", -15920);  
  194.         spellPut("lin", -15915);  
  195.         spellPut("ling", -15903);  
  196.         spellPut("liu", -15889);  
  197.         spellPut("long", -15878);  
  198.         spellPut("lou", -15707);  
  199.         spellPut("lu", -15701);  
  200.         spellPut("lv", -15681);  
  201.         spellPut("luan", -15667);  
  202.         spellPut("lue", -15661);  
  203.         spellPut("lun", -15659);  
  204.         spellPut("luo", -15652);  
  205.         spellPut("ma", -15640);  
  206.         spellPut("mai", -15631);  
  207.         spellPut("man", -15625);  
  208.         spellPut("mang", -15454);  
  209.         spellPut("mao", -15448);  
  210.         spellPut("me", -15436);  
  211.         spellPut("mei", -15435);  
  212.         spellPut("men", -15419);  
  213.         spellPut("meng", -15416);  
  214.         spellPut("mi", -15408);  
  215.         spellPut("mian", -15394);  
  216.         spellPut("miao", -15385);  
  217.         spellPut("mie", -15377);  
  218.         spellPut("min", -15375);  
  219.         spellPut("ming", -15369);  
  220.         spellPut("miu", -15363);  
  221.         spellPut("mo", -15362);  
  222.         spellPut("mou", -15183);  
  223.         spellPut("mu", -15180);  
  224.         spellPut("na", -15165);  
  225.         spellPut("nai", -15158);  
  226.         spellPut("nan", -15153);  
  227.         spellPut("nang", -15150);  
  228.         spellPut("nao", -15149);  
  229.         spellPut("ne", -15144);  
  230.         spellPut("nei", -15143);  
  231.         spellPut("nen", -15141);  
  232.         spellPut("neng", -15140);  
  233.         spellPut("ni", -15139);  
  234.         spellPut("nian", -15128);  
  235.         spellPut("niang", -15121);  
  236.         spellPut("niao", -15119);  
  237.         spellPut("nie", -15117);  
  238.         spellPut("nin", -15110);  
  239.         spellPut("ning", -15109);  
  240.         spellPut("niu", -14941);  
  241.         spellPut("nong", -14937);  
  242.         spellPut("nu", -14933);  
  243.         spellPut("nv", -14930);  
  244.         spellPut("nuan", -14929);  
  245.         spellPut("nue", -14928);  
  246.         spellPut("nuo", -14926);  
  247.         spellPut("o", -14922);  
  248.         spellPut("ou", -14921);  
  249.         spellPut("pa", -14914);  
  250.         spellPut("pai", -14908);  
  251.         spellPut("pan", -14902);  
  252.         spellPut("pang", -14894);  
  253.         spellPut("pao", -14889);  
  254.         spellPut("pei", -14882);  
  255.         spellPut("pen", -14873);  
  256.         spellPut("peng", -14871);  
  257.         spellPut("pi", -14857);  
  258.         spellPut("pian", -14678);  
  259.         spellPut("piao", -14674);  
  260.         spellPut("pie", -14670);  
  261.         spellPut("pin", -14668);  
  262.         spellPut("ping", -14663);  
  263.         spellPut("po", -14654);  
  264.         spellPut("pu", -14645);  
  265.         spellPut("qi", -14630);  
  266.         spellPut("qia", -14594);  
  267.         spellPut("qian", -14429);  
  268.         spellPut("qiang", -14407);  
  269.         spellPut("qiao", -14399);  
  270.         spellPut("qie", -14384);  
  271.         spellPut("qin", -14379);  
  272.         spellPut("qing", -14368);  
  273.         spellPut("qiong", -14355);  
  274.         spellPut("qiu", -14353);  
  275.         spellPut("qu", -14345);  
  276.         spellPut("quan", -14170);  
  277.         spellPut("que", -14159);  
  278.         spellPut("qun", -14151);  
  279.         spellPut("ran", -14149);  
  280.         spellPut("rang", -14145);  
  281.         spellPut("rao", -14140);  
  282.         spellPut("re", -14137);  
  283.         spellPut("ren", -14135);  
  284.         spellPut("reng", -14125);  
  285.         spellPut("ri", -14123);  
  286.         spellPut("rong", -14122);  
  287.         spellPut("rou", -14112);  
  288.         spellPut("ru", -14109);  
  289.         spellPut("ruan", -14099);  
  290.         spellPut("rui", -14097);  
  291.         spellPut("run", -14094);  
  292.         spellPut("ruo", -14092);  
  293.         spellPut("sa", -14090);  
  294.         spellPut("sai", -14087);  
  295.         spellPut("san", -14083);  
  296.         spellPut("sang", -13917);  
  297.         spellPut("sao", -13914);  
  298.         spellPut("se", -13910);  
  299.         spellPut("sen", -13907);  
  300.         spellPut("seng", -13906);  
  301.         spellPut("sha", -13905);  
  302.         spellPut("shai", -13896);  
  303.         spellPut("shan", -13894);  
  304.         spellPut("shang", -13878);  
  305.         spellPut("shao", -13870);  
  306.         spellPut("she", -13859);  
  307.         spellPut("shen", -13847);  
  308.         spellPut("sheng", -13831);  
  309.         spellPut("shi", -13658);  
  310.         spellPut("shou", -13611);  
  311.         spellPut("shu", -13601);  
  312.         spellPut("shua", -13406);  
  313.         spellPut("shuai", -13404);  
  314.         spellPut("shuan", -13400);  
  315.         spellPut("shuang", -13398);  
  316.         spellPut("shui", -13395);  
  317.         spellPut("shun", -13391);  
  318.         spellPut("shuo", -13387);  
  319.         spellPut("si", -13383);  
  320.         spellPut("song", -13367);  
  321.         spellPut("sou", -13359);  
  322.         spellPut("su", -13356);  
  323.         spellPut("suan", -13343);  
  324.         spellPut("sui", -13340);  
  325.         spellPut("sun", -13329);  
  326.         spellPut("suo", -13326);  
  327.         spellPut("ta", -13318);  
  328.         spellPut("tai", -13147);  
  329.         spellPut("tan", -13138);  
  330.         spellPut("tang", -13120);  
  331.         spellPut("tao", -13107);  
  332.         spellPut("te", -13096);  
  333.         spellPut("teng", -13095);  
  334.         spellPut("ti", -13091);  
  335.         spellPut("tian", -13076);  
  336.         spellPut("tiao", -13068);  
  337.         spellPut("tie", -13063);  
  338.         spellPut("ting", -13060);  
  339.         spellPut("tong", -12888);  
  340.         spellPut("tou", -12875);  
  341.         spellPut("tu", -12871);  
  342.         spellPut("tuan", -12860);  
  343.         spellPut("tui", -12858);  
  344.         spellPut("tun", -12852);  
  345.         spellPut("tuo", -12849);  
  346.         spellPut("wa", -12838);  
  347.         spellPut("wai", -12831);  
  348.         spellPut("wan", -12829);  
  349.         spellPut("wang", -12812);  
  350.         spellPut("wei", -12802);  
  351.         spellPut("wen", -12607);  
  352.         spellPut("weng", -12597);  
  353.         spellPut("wo", -12594);  
  354.         spellPut("wu", -12585);  
  355.         spellPut("xi", -12556);  
  356.         spellPut("xia", -12359);  
  357.         spellPut("xian", -12346);  
  358.         spellPut("xiang", -12320);  
  359.         spellPut("xiao", -12300);  
  360.         spellPut("xie", -12120);  
  361.         spellPut("xin", -12099);  
  362.         spellPut("xing", -12089);  
  363.         spellPut("xiong", -12074);  
  364.         spellPut("xiu", -12067);  
  365.         spellPut("xu", -12058);  
  366.         spellPut("xuan", -12039);  
  367.         spellPut("xue", -11867);  
  368.         spellPut("xun", -11861);  
  369.         spellPut("ya", -11847);  
  370.         spellPut("yan", -11831);  
  371.         spellPut("yang", -11798);  
  372.         spellPut("yao", -11781);  
  373.         spellPut("ye", -11604);  
  374.         spellPut("yi", -11589);  
  375.         spellPut("yin", -11536);  
  376.         spellPut("ying", -11358);  
  377.         spellPut("yo", -11340);  
  378.         spellPut("yong", -11339);  
  379.         spellPut("you", -11324);  
  380.         spellPut("yu", -11303);  
  381.         spellPut("yuan", -11097);  
  382.         spellPut("yue", -11077);  
  383.         spellPut("yun", -11067);  
  384.         spellPut("za", -11055);  
  385.         spellPut("zai", -11052);  
  386.         spellPut("zan", -11045);  
  387.         spellPut("zang", -11041);  
  388.         spellPut("zao", -11038);  
  389.         spellPut("ze", -11024);  
  390.         spellPut("zei", -11020);  
  391.         spellPut("zen", -11019);  
  392.         spellPut("zeng", -11018);  
  393.         spellPut("zha", -11014);  
  394.         spellPut("zhai", -10838);  
  395.         spellPut("zhan", -10832);  
  396.         spellPut("zhang", -10815);  
  397.         spellPut("zhao", -10800);  
  398.         spellPut("zhe", -10790);  
  399.         spellPut("zhen", -10780);  
  400.         spellPut("zheng", -10764);  
  401.         spellPut("zhi", -10587);  
  402.         spellPut("zhong", -10544);  
  403.         spellPut("zhou", -10533);  
  404.         spellPut("zhu", -10519);  
  405.         spellPut("zhua", -10331);  
  406.         spellPut("zhuai", -10329);  
  407.         spellPut("zhuan", -10328);  
  408.         spellPut("zhuang", -10322);  
  409.         spellPut("zhui", -10315);  
  410.         spellPut("zhun", -10309);  
  411.         spellPut("zhuo", -10307);  
  412.         spellPut("zi", -10296);  
  413.         spellPut("zong", -10281);  
  414.         spellPut("zou", -10274);  
  415.         spellPut("zu", -10270);  
  416.         spellPut("zuan", -10262);  
  417.         spellPut("zui", -10260);  
  418.         spellPut("zun", -10256);  
  419.         spellPut("zuo", -10254);  
  420.     }  
  421.   
  422.     /** 
  423.      * 获得单个汉字的Ascii. 
  424.      * @param cn char 汉字字符 
  425.      * @return int 错误返回 0,否则返回ascii 
  426.      */  
  427.     public static int getCnAscii(char cn) {  
  428.         byte[] bytes = (String.valueOf(cn)).getBytes();  
  429.         if (bytes == null || bytes.length > 2 || bytes.length <= 0) { //错误  
  430.             return 0;  
  431.         }  
  432.         if (bytes.length == 1) { //英文字符  
  433.             return bytes[0];  
  434.         }  
  435.         if (bytes.length == 2) { //中文字符  
  436.             int hightByte = 256 + bytes[0];  
  437.             int lowByte = 256 + bytes[1];  
  438.   
  439.             int ascii = (256 * hightByte + lowByte) - 256 * 256;  
  440.   
  441.             return ascii;  
  442.         }  
  443.         return 0//错误  
  444.     }  
  445.   
  446.     /** 
  447.      * 根据ASCII码到SpellMap中查找对应的拼音 
  448.      * @param ascii int 
  449.      * 字符对应的ASCII 
  450.      * @return String 
  451.      * 拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符, 
  452.      * <BR>否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音. 
  453.      */  
  454.     public static String getSpellByAscii(int ascii) {  
  455.         if (ascii > 0 && ascii < 160) { //单字符  
  456.             return String.valueOf( (char) ascii);  
  457.         }  
  458.   
  459.         if (ascii < -20319 || ascii > -10247) { //不知道的字符  
  460.             return null;  
  461.         }  
  462.   
  463.         Set keySet = spellMap.keySet();  
  464.         Iterator it = keySet.iterator();  
  465.   
  466.         String spell0 = null;  
  467.         String spell = null;  
  468.   
  469.         int asciiRang0 = -20319;  
  470.         int asciiRang;  
  471.         while (it.hasNext()) {  
  472.             spell = (String) it.next();  
  473.             Object valObj = spellMap.get(spell);  
  474.             if (valObj instanceof Integer) {  
  475.                 asciiRang = ( (Integer) valObj).intValue();  
  476.   
  477.                 if (ascii >= asciiRang0 && ascii < asciiRang) { //区间找到  
  478.                     return (spell0 == null) ? spell : spell0;  
  479.                 }  
  480.                 else {  
  481.                     spell0 = spell;  
  482.                     asciiRang0 = asciiRang;  
  483.                 }  
  484.             }  
  485.         }  
  486.   
  487.         return null;  
  488.   
  489.     }  
  490.   
  491.     /** 
  492.      * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换 
  493.      * @param cnStr String 
  494.      * 字符串 
  495.      * @return String 
  496.      * 转换成全拼后的字符串 
  497.      */  
  498.     public static String getFullSpell(String cnStr) {  
  499.         if (null == cnStr || "".equals(cnStr.trim())) {  
  500.             return cnStr;  
  501.         }  
  502.   
  503.         char[] chars = cnStr.toCharArray();  
  504.         StringBuffer retuBuf = new StringBuffer();  
  505.         for (int i = 0, Len = chars.length; i < Len; i++) {  
  506.             int ascii = getCnAscii(chars[i]);  
  507.             if (ascii == 0) { //取ascii时出错  
  508.                 retuBuf.append(chars[i]);  
  509.             }  
  510.             else {  
  511.                 String spell = getSpellByAscii(ascii);  
  512.                 if (spell == null) {  
  513.                     retuBuf.append(chars[i]);  
  514.                 }  
  515.                 else {  
  516.                     retuBuf.append(spell);  
  517.                 } // end of if spell == null  
  518.             } // end of if ascii <= -20400  
  519.         } // end of for  
  520.   
  521.         return retuBuf.toString();  
  522.     }  
  523.   
  524.     /** 
  525.      * 第一个字取全拼,后面的字取首字母 
  526.      * @param cnStr 
  527.      * @return 返回转换后的拼 
  528.      */  
  529.     public static String getFirstSpell(String cnStr) {  
  530.         if (null == cnStr || "".equals(cnStr.trim())) {  
  531.             return cnStr;  
  532.         }  
  533.   
  534.         char[] chars = cnStr.toCharArray();  
  535.         StringBuffer retuBuf = new StringBuffer();  
  536.         for (int i = 0, Len = chars.length; i < Len; i++) {  
  537.             int ascii = getCnAscii(chars[i]);  
  538.   
  539.             if (ascii == 0) { //取ascii时出错  
  540.                 retuBuf.append(chars[i]);  
  541.             }  
  542.             else {  
  543.                 String spell = getSpellByAscii(ascii);  
  544.                 if (spell == null) {  
  545.                     retuBuf.append(chars[i]);  
  546.                 }  
  547.                 else {  
  548.                     if (i == 0) { //第一个字取全拼  
  549.                         retuBuf.append(spell);  
  550.                     }  
  551.                     else { //后面的字取首字母  
  552.                         retuBuf.append(spell.substring(01));  
  553.                     }  
  554.                 }  
  555.             }  
  556.   
  557.         } // end of for  
  558.         return retuBuf.toString();  
  559.     }  
  560.   
  561.     public static void main(String[] args) {  
  562.         String str = "中华人明共和国Haha";  
  563.         // 转换成拼音  
  564.         System.out.println(ChineseToEnglish2.getFullSpell(str));  
  565.         // 中文第一个字全拼,其余去首字母  
  566.         System.out.println(ChineseToEnglish2.getFirstSpell(str));  
  567.         // ascii 转 字母  
  568.         System.out.println(ChineseToEnglish2.getSpellByAscii(65));  
  569.         // 字母 转 ascii  
  570.         System.out.println(ChineseToEnglish2.getCnAscii('0'));  
  571.     }  
  572. }  
  573. 1.使用pinyin4j将汉字转成拼音,附件为pinyin4j的jar包 
    Java代码  收藏代码
    1. import net.sourceforge.pinyin4j.PinyinHelper;  
    2. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
    3. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
    4. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
    5. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
    6. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
    7.   
    8. public class ChineseToEnglish {  
    9.     // 将汉字转换为全拼  
    10.     public static String getPingYin(String src) {  
    11.   
    12.         char[] t1 = null;  
    13.         t1 = src.toCharArray();  
    14.         String[] t2 = new String[t1.length];  
    15.         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
    16.           
    17.         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
    18.         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
    19.         t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
    20.         String t4 = "";  
    21.         int t0 = t1.length;  
    22.         try {  
    23.             for (int i = 0; i < t0; i++) {  
    24.                 // 判断是否为汉字字符  
    25.                 if (java.lang.Character.toString(t1[i]).matches(  
    26.                         "[\\u4E00-\\u9FA5]+")) {  
    27.                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);  
    28.                     t4 += t2[0];  
    29.                 } else  
    30.                     t4 += java.lang.Character.toString(t1[i]);  
    31.             }  
    32.             // System.out.println(t4);  
    33.             return t4;  
    34.         } catch (BadHanyuPinyinOutputFormatCombination e1) {  
    35.             e1.printStackTrace();  
    36.         }  
    37.         return t4;  
    38.     }  
    39.   
    40.     // 返回中文的首字母  
    41.     public static String getPinYinHeadChar(String str) {  
    42.   
    43.         String convert = "";  
    44.         for (int j = 0; j < str.length(); j++) {  
    45.             char word = str.charAt(j);  
    46.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
    47.             if (pinyinArray != null) {  
    48.                 convert += pinyinArray[0].charAt(0);  
    49.             } else {  
    50.                 convert += word;  
    51.             }  
    52.         }  
    53.         return convert;  
    54.     }  
    55.   
    56.     // 将字符串转移为ASCII码  
    57.     public static String getCnASCII(String cnStr) {  
    58.         StringBuffer strBuf = new StringBuffer();  
    59.         byte[] bGBK = cnStr.getBytes();  
    60.         for (int i = 0; i < bGBK.length; i++) {  
    61.             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));  
    62.         }  
    63.         return strBuf.toString();  
    64.     }  
    65.   
    66.     public static void main(String[] args) {  
    67.         System.out.println(getPingYin("綦江qq县"));  
    68.         System.out.println(getPinYinHeadChar("綦江县"));  
    69.         System.out.println(getCnASCII("綦江县"));  
    70.     }  
    71. }  


    2.java程序直接转 
    Java代码  收藏代码
    1. import java.util.Iterator;  
    2. import java.util.LinkedHashMap;  
    3. import java.util.Set;  
    4.   
    5. public class ChineseToEnglish2 {  
    6.     private static LinkedHashMap spellMap = null;  
    7.   
    8.     static {  
    9.         if (spellMap == null) {  
    10.             spellMap = new LinkedHashMap(500);  
    11.         }  
    12.         initialize();  
    13.     }  
    14.   
    15.     private ChineseToEnglish2() {  
    16.   
    17.     }  
    18.   
    19.     private static void spellPut(String spell, int ascii) {  
    20.         spellMap.put(spell, new Integer(ascii));  
    21.     }  
    22.   
    23.     private static void initialize() {  
    24.         spellPut("a", -20319);  
    25.         spellPut("ai", -20317);  
    26.         spellPut("an", -20304);  
    27.         spellPut("ang", -20295);  
    28.         spellPut("ao", -20292);  
    29.         spellPut("ba", -20283);  
    30.         spellPut("bai", -20265);  
    31.         spellPut("ban", -20257);  
    32.         spellPut("bang", -20242);  
    33.         spellPut("bao", -20230);  
    34.         spellPut("bei", -20051);  
    35.         spellPut("ben", -20036);  
    36.         spellPut("beng", -20032);  
    37.         spellPut("bi", -20026);  
    38.         spellPut("bian", -20002);  
    39.         spellPut("biao", -19990);  
    40.         spellPut("bie", -19986);  
    41.         spellPut("bin", -19982);  
    42.         spellPut("bing", -19976);  
    43.         spellPut("bo", -19805);  
    44.         spellPut("bu", -19784);  
    45.         spellPut("ca", -19775);  
    46.         spellPut("cai", -19774);  
    47.         spellPut("can", -19763);  
    48.         spellPut("cang", -19756);  
    49.         spellPut("cao", -19751);  
    50.         spellPut("ce", -19746);  
    51.         spellPut("ceng", -19741);  
    52.         spellPut("cha", -19739);  
    53.         spellPut("chai", -19728);  
    54.         spellPut("chan", -19725);  
    55.         spellPut("chang", -19715);  
    56.         spellPut("chao", -19540);  
    57.         spellPut("che", -19531);  
    58.         spellPut("chen", -19525);  
    59.         spellPut("cheng", -19515);  
    60.         spellPut("chi", -19500);  
    61.         spellPut("chong", -19484);  
    62.         spellPut("chou", -19479);  
    63.         spellPut("chu", -19467);  
    64.         spellPut("chuai", -19289);  
    65.         spellPut("chuan", -19288);  
    66.         spellPut("chuang", -19281);  
    67.         spellPut("chui", -19275);  
    68.         spellPut("chun", -19270);  
    69.         spellPut("chuo", -19263);  
    70.         spellPut("ci", -19261);  
    71.         spellPut("cong", -19249);  
    72.         spellPut("cou", -19243);  
    73.         spellPut("cu", -19242);  
    74.         spellPut("cuan", -19238);  
    75.         spellPut("cui", -19235);  
    76.         spellPut("cun", -19227);  
    77.         spellPut("cuo", -19224);  
    78.         spellPut("da", -19218);  
    79.         spellPut("dai", -19212);  
    80.         spellPut("dan", -19038);  
    81.         spellPut("dang", -19023);  
    82.         spellPut("dao", -19018);  
    83.         spellPut("de", -19006);  
    84.         spellPut("deng", -19003);  
    85.         spellPut("di", -18996);  
    86.         spellPut("dian", -18977);  
    87.         spellPut("diao", -18961);  
    88.         spellPut("die", -18952);  
    89.         spellPut("ding", -18783);  
    90.         spellPut("diu", -18774);  
    91.         spellPut("dong", -18773);  
    92.         spellPut("dou", -18763);  
    93.         spellPut("du", -18756);  
    94.         spellPut("duan", -18741);  
    95.         spellPut("dui", -18735);  
    96.         spellPut("dun", -18731);  
    97.         spellPut("duo", -18722);  
    98.         spellPut("e", -18710);  
    99.         spellPut("en", -18697);  
    100.         spellPut("er", -18696);  
    101.         spellPut("fa", -18526);  
    102.         spellPut("fan", -18518);  
    103.         spellPut("fang", -18501);  
    104.         spellPut("fei", -18490);  
    105.         spellPut("fen", -18478);  
    106.         spellPut("feng", -18463);  
    107.         spellPut("fo", -18448);  
    108.         spellPut("fou", -18447);  
    109.         spellPut("fu", -18446);  
    110.         spellPut("ga", -18239);  
    111.         spellPut("gai", -18237);  
    112.         spellPut("gan", -18231);  
    113.         spellPut("gang", -18220);  
    114.         spellPut("gao", -18211);  
    115.         spellPut("ge", -18201);  
    116.         spellPut("gei", -18184);  
    117.         spellPut("gen", -18183);  
    118.         spellPut("geng", -18181);  
    119.         spellPut("gong", -18012);  
    120.         spellPut("gou", -17997);  
    121.         spellPut("gu", -17988);  
    122.         spellPut("gua", -17970);  
    123.         spellPut("guai", -17964);  
    124.         spellPut("guan", -17961);  
    125.         spellPut("guang", -17950);  
    126.         spellPut("gui", -17947);  
    127.         spellPut("gun", -17931);  
    128.         spellPut("guo", -17928);  
    129.         spellPut("ha", -17922);  
    130.         spellPut("hai", -17759);  
    131.         spellPut("han", -17752);  
    132.         spellPut("hang", -17733);  
    133.         spellPut("hao", -17730);  
    134.         spellPut("he", -17721);  
    135.         spellPut("hei", -17703);  
    136.         spellPut("hen", -17701);  
    137.         spellPut("heng", -17697);  
    138.         spellPut("hong", -17692);  
    139.         spellPut("hou", -17683);  
    140.         spellPut("hu", -17676);  
    141.         spellPut("hua", -17496);  
    142.         spellPut("huai", -17487);  
    143.         spellPut("huan", -17482);  
    144.         spellPut("huang", -17468);  
    145.         spellPut("hui", -17454);  
    146.         spellPut("hun", -17433);  
    147.         spellPut("huo", -17427);  
    148.         spellPut("ji", -17417);  
    149.         spellPut("jia", -17202);  
    150.         spellPut("jian", -17185);  
    151.         spellPut("jiang", -16983);  
    152.         spellPut("jiao", -16970);  
    153.         spellPut("jie", -16942);  
    154.         spellPut("jin", -16915);  
    155.         spellPut("jing", -16733);  
    156.         spellPut("jiong", -16708);  
    157.         spellPut("jiu", -16706);  
    158.         spellPut("ju", -16689);  
    159.         spellPut("juan", -16664);  
    160.         spellPut("jue", -16657);  
    161.         spellPut("jun", -16647);  
    162.         spellPut("ka", -16474);  
    163.         spellPut("kai", -16470);  
    164.         spellPut("kan", -16465);  
    165.         spellPut("kang", -16459);  
    166.         spellPut("kao", -16452);  
    167.         spellPut("ke", -16448);  
    168.         spellPut("ken", -16433);  
    169.         spellPut("keng", -16429);  
    170.         spellPut("kong", -16427);  
    171.         spellPut("kou", -16423);  
    172.         spellPut("ku", -16419);  
    173.         spellPut("kua", -16412);  
    174.         spellPut("kuai", -16407);  
    175.         spellPut("kuan", -16403);  
    176.         spellPut("kuang", -16401);  
    177.         spellPut("kui", -16393);  
    178.         spellPut("kun", -16220);  
    179.         spellPut("kuo", -16216);  
    180.         spellPut("la", -16212);  
    181.         spellPut("lai", -16205);  
    182.         spellPut("lan", -16202);  
    183.         spellPut("lang", -16187);  
    184.         spellPut("lao", -16180);  
    185.         spellPut("le", -16171);  
    186.         spellPut("lei", -16169);  
    187.         spellPut("leng", -16158);  
    188.         spellPut("li", -16155);  
    189.         spellPut("lia", -15959);  
    190.         spellPut("lian", -15958);  
    191.         spellPut("liang", -15944);  
    192.         spellPut("liao", -15933);  
    193.         spellPut("lie", -15920);  
    194.         spellPut("lin", -15915);  
    195.         spellPut("ling", -15903);  
    196.         spellPut("liu", -15889);  
    197.         spellPut("long", -15878);  
    198.         spellPut("lou", -15707);  
    199.         spellPut("lu", -15701);  
    200.         spellPut("lv", -15681);  
    201.         spellPut("luan", -15667);  
    202.         spellPut("lue", -15661);  
    203.         spellPut("lun", -15659);  
    204.         spellPut("luo", -15652);  
    205.         spellPut("ma", -15640);  
    206.         spellPut("mai", -15631);  
    207.         spellPut("man", -15625);  
    208.         spellPut("mang", -15454);  
    209.         spellPut("mao", -15448);  
    210.         spellPut("me", -15436);  
    211.         spellPut("mei", -15435);  
    212.         spellPut("men", -15419);  
    213.         spellPut("meng", -15416);  
    214.         spellPut("mi", -15408);  
    215.         spellPut("mian", -15394);  
    216.         spellPut("miao", -15385);  
    217.         spellPut("mie", -15377);  
    218.         spellPut("min", -15375);  
    219.         spellPut("ming", -15369);  
    220.         spellPut("miu", -15363);  
    221.         spellPut("mo", -15362);  
    222.         spellPut("mou", -15183);  
    223.         spellPut("mu", -15180);  
    224.         spellPut("na", -15165);  
    225.         spellPut("nai", -15158);  
    226.         spellPut("nan", -15153);  
    227.         spellPut("nang", -15150);  
    228.         spellPut("nao", -15149);  
    229.         spellPut("ne", -15144);  
    230.         spellPut("nei", -15143);  
    231.         spellPut("nen", -15141);  
    232.         spellPut("neng", -15140);  
    233.         spellPut("ni", -15139);  
    234.         spellPut("nian", -15128);  
    235.         spellPut("niang", -15121);  
    236.         spellPut("niao", -15119);  
    237.         spellPut("nie", -15117);  
    238.         spellPut("nin", -15110);  
    239.         spellPut("ning", -15109);  
    240.         spellPut("niu", -14941);  
    241.         spellPut("nong", -14937);  
    242.         spellPut("nu", -14933);  
    243.         spellPut("nv", -14930);  
    244.         spellPut("nuan", -14929);  
    245.         spellPut("nue", -14928);  
    246.         spellPut("nuo", -14926);  
    247.         spellPut("o", -14922);  
    248.         spellPut("ou", -14921);  
    249.         spellPut("pa", -14914);  
    250.         spellPut("pai", -14908);  
    251.         spellPut("pan", -14902);  
    252.         spellPut("pang", -14894);  
    253.         spellPut("pao", -14889);  
    254.         spellPut("pei", -14882);  
    255.         spellPut("pen", -14873);  
    256.         spellPut("peng", -14871);  
    257.         spellPut("pi", -14857);  
    258.         spellPut("pian", -14678);  
    259.         spellPut("piao", -14674);  
    260.         spellPut("pie", -14670);  
    261.         spellPut("pin", -14668);  
    262.         spellPut("ping", -14663);  
    263.         spellPut("po", -14654);  
    264.         spellPut("pu", -14645);  
    265.         spellPut("qi", -14630);  
    266.         spellPut("qia", -14594);  
    267.         spellPut("qian", -14429);  
    268.         spellPut("qiang", -14407);  
    269.         spellPut("qiao", -14399);  
    270.         spellPut("qie", -14384);  
    271.         spellPut("qin", -14379);  
    272.         spellPut("qing", -14368);  
    273.         spellPut("qiong", -14355);  
    274.         spellPut("qiu", -14353);  
    275.         spellPut("qu", -14345);  
    276.         spellPut("quan", -14170);  
    277.         spellPut("que", -14159);  
    278.         spellPut("qun", -14151);  
    279.         spellPut("ran", -14149);  
    280.         spellPut("rang", -14145);  
    281.         spellPut("rao", -14140);  
    282.         spellPut("re", -14137);  
    283.         spellPut("ren", -14135);  
    284.         spellPut("reng", -14125);  
    285.         spellPut("ri", -14123);  
    286.         spellPut("rong", -14122);  
    287.         spellPut("rou", -14112);  
    288.         spellPut("ru", -14109);  
    289.         spellPut("ruan", -14099);  
    290.         spellPut("rui", -14097);  
    291.         spellPut("run", -14094);  
    292.         spellPut("ruo", -14092);  
    293.         spellPut("sa", -14090);  
    294.         spellPut("sai", -14087);  
    295.         spellPut("san", -14083);  
    296.         spellPut("sang", -13917);  
    297.         spellPut("sao", -13914);  
    298.         spellPut("se", -13910);  
    299.         spellPut("sen", -13907);  
    300.         spellPut("seng", -13906);  
    301.         spellPut("sha", -13905);  
    302.         spellPut("shai", -13896);  
    303.         spellPut("shan", -13894);  
    304.         spellPut("shang", -13878);  
    305.         spellPut("shao", -13870);  
    306.         spellPut("she", -13859);  
    307.         spellPut("shen", -13847);  
    308.         spellPut("sheng", -13831);  
    309.         spellPut("shi", -13658);  
    310.         spellPut("shou", -13611);  
    311.         spellPut("shu", -13601);  
    312.         spellPut("shua", -13406);  
    313.         spellPut("shuai", -13404);  
    314.         spellPut("shuan", -13400);  
    315.         spellPut("shuang", -13398);  
    316.         spellPut("shui", -13395);  
    317.         spellPut("shun", -13391);  
    318.         spellPut("shuo", -13387);  
    319.         spellPut("si", -13383);  
    320.         spellPut("song", -13367);  
    321.         spellPut("sou", -13359);  
    322.         spellPut("su", -13356);  
    323.         spellPut("suan", -13343);  
    324.         spellPut("sui", -13340);  
    325.         spellPut("sun", -13329);  
    326.         spellPut("suo", -13326);  
    327.         spellPut("ta", -13318);  
    328.         spellPut("tai", -13147);  
    329.         spellPut("tan", -13138);  
    330.         spellPut("tang", -13120);  
    331.         spellPut("tao", -13107);  
    332.         spellPut("te", -13096);  
    333.         spellPut("teng", -13095);  
    334.         spellPut("ti", -13091);  
    335.         spellPut("tian", -13076);  
    336.         spellPut("tiao", -13068);  
    337.         spellPut("tie", -13063);  
    338.         spellPut("ting", -13060);  
    339.         spellPut("tong", -12888);  
    340.         spellPut("tou", -12875);  
    341.         spellPut("tu", -12871);  
    342.         spellPut("tuan", -12860);  
    343.         spellPut("tui", -12858);  
    344.         spellPut("tun", -12852);  
    345.         spellPut("tuo", -12849);  
    346.         spellPut("wa", -12838);  
    347.         spellPut("wai", -12831);  
    348.         spellPut("wan", -12829);  
    349.         spellPut("wang", -12812);  
    350.         spellPut("wei", -12802);  
    351.         spellPut("wen", -12607);  
    352.         spellPut("weng", -12597);  
    353.         spellPut("wo", -12594);  
    354.         spellPut("wu", -12585);  
    355.         spellPut("xi", -12556);  
    356.         spellPut("xia", -12359);  
    357.         spellPut("xian", -12346);  
    358.         spellPut("xiang", -12320);  
    359.         spellPut("xiao", -12300);  
    360.         spellPut("xie", -12120);  
    361.         spellPut("xin", -12099);  
    362.         spellPut("xing", -12089);  
    363.         spellPut("xiong", -12074);  
    364.         spellPut("xiu", -12067);  
    365.         spellPut("xu", -12058);  
    366.         spellPut("xuan", -12039);  
    367.         spellPut("xue", -11867);  
    368.         spellPut("xun", -11861);  
    369.         spellPut("ya", -11847);  
    370.         spellPut("yan", -11831);  
    371.         spellPut("yang", -11798);  
    372.         spellPut("yao", -11781);  
    373.         spellPut("ye", -11604);  
    374.         spellPut("yi", -11589);  
    375.         spellPut("yin", -11536);  
    376.         spellPut("ying", -11358);  
    377.         spellPut("yo", -11340);  
    378.         spellPut("yong", -11339);  
    379.         spellPut("you", -11324);  
    380.         spellPut("yu", -11303);  
    381.         spellPut("yuan", -11097);  
    382.         spellPut("yue", -11077);  
    383.         spellPut("yun", -11067);  
    384.         spellPut("za", -11055);  
    385.         spellPut("zai", -11052);  
    386.         spellPut("zan", -11045);  
    387.         spellPut("zang", -11041);  
    388.         spellPut("zao", -11038);  
    389.         spellPut("ze", -11024);  
    390.         spellPut("zei", -11020);  
    391.         spellPut("zen", -11019);  
    392.         spellPut("zeng", -11018);  
    393.         spellPut("zha", -11014);  
    394.         spellPut("zhai", -10838);  
    395.         spellPut("zhan", -10832);  
    396.         spellPut("zhang", -10815);  
    397.         spellPut("zhao", -10800);  
    398.         spellPut("zhe", -10790);  
    399.         spellPut("zhen", -10780);  
    400.         spellPut("zheng", -10764);  
    401.         spellPut("zhi", -10587);  
    402.         spellPut("zhong", -10544);  
    403.         spellPut("zhou", -10533);  
    404.         spellPut("zhu", -10519);  
    405.         spellPut("zhua", -10331);  
    406.         spellPut("zhuai", -10329);  
    407.         spellPut("zhuan", -10328);  
    408.         spellPut("zhuang", -10322);  
    409.         spellPut("zhui", -10315);  
    410.         spellPut("zhun", -10309);  
    411.         spellPut("zhuo", -10307);  
    412.         spellPut("zi", -10296);  
    413.         spellPut("zong", -10281);  
    414.         spellPut("zou", -10274);  
    415.         spellPut("zu", -10270);  
    416.         spellPut("zuan", -10262);  
    417.         spellPut("zui", -10260);  
    418.         spellPut("zun", -10256);  
    419.         spellPut("zuo", -10254);  
    420.     }  
    421.   
    422.     /** 
    423.      * 获得单个汉字的Ascii. 
    424.      * @param cn char 汉字字符 
    425.      * @return int 错误返回 0,否则返回ascii 
    426.      */  
    427.     public static int getCnAscii(char cn) {  
    428.         byte[] bytes = (String.valueOf(cn)).getBytes();  
    429.         if (bytes == null || bytes.length > 2 || bytes.length <= 0) { //错误  
    430.             return 0;  
    431.         }  
    432.         if (bytes.length == 1) { //英文字符  
    433.             return bytes[0];  
    434.         }  
    435.         if (bytes.length == 2) { //中文字符  
    436.             int hightByte = 256 + bytes[0];  
    437.             int lowByte = 256 + bytes[1];  
    438.   
    439.             int ascii = (256 * hightByte + lowByte) - 256 * 256;  
    440.   
    441.             return ascii;  
    442.         }  
    443.         return 0//错误  
    444.     }  
    445.   
    446.     /** 
    447.      * 根据ASCII码到SpellMap中查找对应的拼音 
    448.      * @param ascii int 
    449.      * 字符对应的ASCII 
    450.      * @return String 
    451.      * 拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符, 
    452.      * <BR>否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音. 
    453.      */  
    454.     public static String getSpellByAscii(int ascii) {  
    455.         if (ascii > 0 && ascii < 160) { //单字符  
    456.             return String.valueOf( (char) ascii);  
    457.         }  
    458.   
    459.         if (ascii < -20319 || ascii > -10247) { //不知道的字符  
    460.             return null;  
    461.         }  
    462.   
    463.         Set keySet = spellMap.keySet();  
    464.         Iterator it = keySet.iterator();  
    465.   
    466.         String spell0 = null;  
    467.         String spell = null;  
    468.   
    469.         int asciiRang0 = -20319;  
    470.         int asciiRang;  
    471.         while (it.hasNext()) {  
    472.             spell = (String) it.next();  
    473.             Object valObj = spellMap.get(spell);  
    474.             if (valObj instanceof Integer) {  
    475.                 asciiRang = ( (Integer) valObj).intValue();  
    476.   
    477.                 if (ascii >= asciiRang0 && ascii < asciiRang) { //区间找到  
    478.                     return (spell0 == null) ? spell : spell0;  
    479.                 }  
    480.                 else {  
    481.                     spell0 = spell;  
    482.                     asciiRang0 = asciiRang;  
    483.                 }  
    484.             }  
    485.         }  
    486.   
    487.         return null;  
    488.   
    489.     }  
    490.   
    491.     /** 
    492.      * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换 
    493.      * @param cnStr String 
    494.      * 字符串 
    495.      * @return String 
    496.      * 转换成全拼后的字符串 
    497.      */  
    498.     public static String getFullSpell(String cnStr) {  
    499.         if (null == cnStr || "".equals(cnStr.trim())) {  
    500.             return cnStr;  
    501.         }  
    502.   
    503.         char[] chars = cnStr.toCharArray();  
    504.         StringBuffer retuBuf = new StringBuffer();  
    505.         for (int i = 0, Len = chars.length; i < Len; i++) {  
    506.             int ascii = getCnAscii(chars[i]);  
    507.             if (ascii == 0) { //取ascii时出错  
    508.                 retuBuf.append(chars[i]);  
    509.             }  
    510.             else {  
    511.                 String spell = getSpellByAscii(ascii);  
    512.                 if (spell == null) {  
    513.                     retuBuf.append(chars[i]);  
    514.                 }  
    515.                 else {  
    516.                     retuBuf.append(spell);  
    517.                 } // end of if spell == null  
    518.             } // end of if ascii <= -20400  
    519.         } // end of for  
    520.   
    521.         return retuBuf.toString();  
    522.     }  
    523.   
    524.     /** 
    525.      * 第一个字取全拼,后面的字取首字母 
    526.      * @param cnStr 
    527.      * @return 返回转换后的拼 
    528.      */  
    529.     public static String getFirstSpell(String cnStr) {  
    530.         if (null == cnStr || "".equals(cnStr.trim())) {  
    531.             return cnStr;  
    532.         }  
    533.   
    534.         char[] chars = cnStr.toCharArray();  
    535.         StringBuffer retuBuf = new StringBuffer();  
    536.         for (int i = 0, Len = chars.length; i < Len; i++) {  
    537.             int ascii = getCnAscii(chars[i]);  
    538.   
    539.             if (ascii == 0) { //取ascii时出错  
    540.                 retuBuf.append(chars[i]);  
    541.             }  
    542.             else {  
    543.                 String spell = getSpellByAscii(ascii);  
    544.                 if (spell == null) {  
    545.                     retuBuf.append(chars[i]);  
    546.                 }  
    547.                 else {  
    548.                     if (i == 0) { //第一个字取全拼  
    549.                         retuBuf.append(spell);  
    550.                     }  
    551.                     else { //后面的字取首字母  
    552.                         retuBuf.append(spell.substring(01));  
    553.                     }  
    554.                 }  
    555.             }  
    556.   
    557.         } // end of for  
    558.         return retuBuf.toString();  
    559.     }  
    560.   
    561.     public static void main(String[] args) {  
    562.         String str = "中华人明共和国Haha";  
    563.         // 转换成拼音  
    564.         System.out.println(ChineseToEnglish2.getFullSpell(str));  
    565.         // 中文第一个字全拼,其余去首字母  
    566.         System.out.println(ChineseToEnglish2.getFirstSpell(str));  
    567.         // ascii 转 字母  
    568.         System.out.println(ChineseToEnglish2.getSpellByAscii(65));  
    569.         // 字母 转 ascii  
    570.         System.out.println(ChineseToEnglish2.getCnAscii('0'));  
    571.     }  
    572. }  
    1. 注:该文章是复制的别人的,觉得有需要,所以借鉴过来 分享给大家!



原创粉丝点击