学习网考试学习资料

Gzu521.com

J2SE综合:JAVA实现把汉字转化成拼音(2)(1)

JAVA认证   点击:次   发布时间:2007-1-31   【字体: 】   来源:Gzu521.com
Gzu521.com我的学习网

/**
* 获得单个汉字的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; //错误
}

/**
* 根据ascii码到spellmap中查找对应的拼音
* @param ascii int
* 字符对应的ascii
* @return string
* 拼音,首先判断ascii是否>0%26amp;<160,如果是返回对应的字符,
*
否则到spellmap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.
*/
public static string getspellbyascii(int ascii)
{
if(ascii > 0 %26amp;%26amp; ascii < 160){ //单字符
return string.valueof((char)ascii);
}

if(ascii < -20319 || ascii > -10247){ //不知道的字符
return null;
}

set keyset = spellmap.keyset();
iterator it = keyset.iterator();

string spell0 = null;;
string spell = null;

int asciirang0 = -20319;
int asciirang;
while(it.hasnext()){

spell = (string)it.next();
object valobj = spellmap.get(spell);
if(valobj instanceof integer){
asciirang = ((integer)valobj).intvalue();

if(ascii >= asciirang0 %26amp;%26amp; ascii < asciirang){ //区间找到
return(spell0 == null) ? spell : spell0;
}
else{
spell0 = spell;
asciirang0 = asciirang;
}
}
}

return null;

}

下 一 页
2页: 第 [1] [2]

责任编辑:gzu521

IT认证分类
计算机软件水平考试
全国计算机等级考试
思科认证
微软认证
ORACLE/CIW认证
Linux认证
JAVA认证
其它认证
分类推荐信息
更多...
大类最新文章
更多...