/** *//**
#############################################################################
# DESCRIBE 将汉字转化成拼音
# DATE 2006-7-12
# COMPANY FLX
# PORJECT JAVA
#############################################################################
*/
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
public class CnToSpell {
private static LinkedHashMap spellMap = null;
static {
if (spellMap == null) {
spellMap = new
LinkedHashMap(400);
}
initialize();
System.out.println("Chinese transfer Spell
Done.");
}
private CnToSpell() {
}
/** *//**
* 获得单个汉字的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;
return ascii;
}
return 0; //错误
}
/** *//**
*
返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
* @param cnStr String
* 字符串
* @return String
* 转换成全拼后的字符串
*/
public static String getFullSpell(String cnStr)
{
if (null == cnStr || "".equals(cnStr.trim()))
{
return cnStr;
}
char[] chars = cnStr.toCharArray();
StringBuffer retuBuf = new StringBuffer();
for (int i = 0, Len = chars.length; i < Len;
i++) {
int ascii =
getCnAscii(chars[i]);
if (ascii == 0) {
//取ascii时出错
retuBuf.append(chars[i]);
} else {
String spell
= getSpellByAscii(ascii);
if (spell ==
null) {
retuBuf.append(chars[i]);
} else
{
retuBuf.append(spell);
} // end of
if spell == null
} // end of if ascii <=
-20400
} // end of for
return retuBuf.toString();
}
/** *//**
* 根据ASCII码到SpellMap中查找对应的拼音
* @param ascii int