汉字转拼音

阅读更多
在网上参考了一些汉字转换到拼音的资料。思路应该只有以下两种。
1,查表法。这样做需要一个庞大的映射表,在j2me环境下不大合适。不过效果好,有些还支持多音字。
2,使用GB字库的映射关系。因为GB2312及其扩展GBK的汉字编码都根据区位于拼音存在映射关系。
实际上网络上的大部分文章都是根据第二种方法来实现的。

我也是采用这种方法,因为它基本上可以利用GB2312字库,直接映射成拼音。
如果遇到不支持GB2312的手机,请参考上一篇文章。
http://blog.csdn.net/hunhun1981/archive/2007/10/26/1845576.aspx

首先来介绍下原始的代码,由于这个代码在网上存在多个版本,并不知道原始作者是谁。
再次鄙视哪些剽窃人家东西不留名的人。
http://hibernate.blogdriver.com/hibernate/1036902.html
或者各位可以在百度中搜索“java 中文 拼音”,即可找到很多帖子,基本全是这份代码。

以下是一个重要片段。
  /**
     * 获得单个汉字的Ascii.
     * 
@param  cn char
     * 汉字字符
     * 
@return  int
     * 错误返回 0,否则返回ascii
     
*/
    
public   static   int  getCnAscii( char  cn) {
        
byte [] bytes  =  (String.valueOf(cn)).getBytes();
        
if  (bytes  ==   null   ||  bytes.length  >   2   ||  bytes.length  <=   0 ) {  // 错误
             return   0 ;
        }
        
if  (bytes.length  ==   1 ) {  // 英文字符
             return  bytes[ 0 ];
        }
        
if  (bytes.length  ==   2 ) {  // 中文字符
             int  hightByte  =   256   +  bytes[ 0 ];
            
int  lowByte  =   256   +  bytes[ 1 ];
            
int  ascii  =  ( 256   *  hightByte  +  lowByte)  -   256   *   256 ;
            
// System.out.println("ASCII=" + ascii);
             return  ascii;
        }
        
return   0 // 错误
    }


这个方法中有一个片段大家需要注意,“(String.valueOf(cn)).getBytes();”这一句。
这里获得的应该是GBK编码,可不是unicode。
unicode是无法使用现在的方法来获取拼音的,只能用查表法,弄一张对于j2me来说很庞大的映射表。

下面详细介绍一下我改进后的方法。并且对原始代码的作者表示感谢,虽然不知道他是谁。


public   class  HGB2PINYIN {
 
        
private  String[] name  =  {  " zuo " " zun " " zui " " zuan " " zu " " zou " ,
                
" zong " " zi " " zhuo " " zhun " " zhui " " zhuang " " zhuan " ,
                
" zhuai " " zhua " " zhu " " zhou " " zhong " " zhi " " zheng " ,
                
" zhen " " zhe " " zhao " " zhang " " zhan " " zhai " " zha " " zeng " ,
                
" zen " " zei " " ze " " zao " " zang " " zan " " zai " " za " " yun " ,
                
" yue " " yuan " " yu " " you " " yong " " yo " " ying " " yin " " yi " ,
                
" ye " " yao " " yang " " yan " " ya " " xun " " xue " " xuan " " xu " ,
                
" xiu " " xiong " " xing " " xin " " xie " " xiao " " xiang " " xian " ,
                
" xia " " xi " " wu " " wo " " weng " " wen " " wei " " wang " " wan " ,
                
" wai " " wa " " tuo " " tun " " tui " " tuan " " tu " " tou " " tong " ,
                
" ting " " tie " " tiao " " tian " " ti " " teng " " te " " tao " ,
                
" tang " " tan " " tai " " ta " " suo " " sun " " sui " " suan " " su " ,
                
" sou " " song " " si " " shuo " " shun " " shui " " shuang " " shuan " ,
                
" shuai " " shua " " shu " " shou " " shi " " sheng " " shen " " she " ,
                
" shao " " shang " " shan " " shai " " sha " " seng " " sen " " se " ,
                
" sao " " sang " " san " " sai " " sa " " ruo " " run " " rui " " ruan " ,
                
" ru " " rou " " rong " " ri " " reng " " ren " " re " " rao " " rang " ,
                
" ran " " qun " " que " " quan " " qu " " qiu " " qiong " " qing " ,
                
" qin " " qie " " qiao " " qiang " " qian " " qia " " qi " " pu " " po " ,
                
" ping " " pin " " pie " " piao " " pian " " pi " " peng " " pen " ,
                
" pei " " pao " " pang " " pan " " pai " " pa " " ou " " o " " nuo " ,
                
" nue " " nuan " " nv " " nu " " nong " " niu " " ning " " nin " " nie " ,
                
" niao " " niang " " nian " " ni " " neng " " nen " " nei " " ne " ,
                
" nao " " nang " " nan " " nai " " na " " mu " " mou " " mo " " miu " ,
                
" ming " " min " " mie " " miao " " mian " " mi " " meng " " men " ,
                
" mei " " me " " mao " " mang " " man " " mai " " ma " " luo " " lun " ,
                
" lue " " luan " " lv " " lu " " lou " " long " " liu " " ling " " lin " ,
                
" lie " " liao " " liang " " lian " " lia " " li " " leng " " lei " ,
                
" le " " lao " " lang " " lan " " lai " " la " " kuo " " kun " " kui " ,
                
" kuang " " kuan " " kuai " " kua " " ku " " kou " " kong " " keng " ,
                
" ken " " ke " " kao " " kang " " kan " " kai " " ka " " jun " " jue " ,
                
" juan " " ju " " jiu " " jiong " " jing " " jin " " jie " " jiao " ,
                
" jiang " " jian " " jia " " ji " " huo " " hun " " hui " " huang " ,
                
" huan " " huai " " hua " " hu " " hou " " hong " " heng " " hen " ,
                
" hei " " he " " hao " " hang " " han " " hai " " ha " " guo " " gun " ,
                
" gui " " guang " " guan " " guai " " gua " " gu " " gou " " gong " ,
                
" geng " " gen " " gei " " ge " " gao " " gang " " gan " " gai " " ga " ,
                
" fu " " fou " " fo " " feng " " fen " " fei " " fang " " fan " " fa " ,
                
" er " " en " " e " " duo " " dun " " dui " " duan " " du "

你可能感兴趣的:(Hibernate,百度,SUN,.net,Blog)