/** *//**

#############################################################################

# 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

* 字符对应的ASCII

* @return String

*

拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符,

*

否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.

*/

public static String getSpellByAscii(int ascii)

{

if (ascii > 0 && 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 && ascii < asciiRang) {

//区间找到

return (spell0 == null) ? spell : spell0;

} else

{

spell0 = spell;

asciiRang0 = asciiRang;

}

}

}

return null;

}

private static void initialize() {

spellPut("a", -20319);

spellPut("ai", -20317);

spellPut("an", -20304);

spellPut("ang", -20295);

spellPut("ao", -20292);

spellPut("ba", -20283);

spellPut("bai", -20265);

spellPut("ban", -20257);

spellPut("bang", -20242);

spellPut("bao", -20230);

spellPut("bei", -20051);

spellPut("ben", -20036);

spellPut("beng", -20032);

spellPut("bi", -20026);

spellPut("bian", -20002);

spellPut("biao", -19990);

spellPut("bie", -19986);

spellPut("bin", -19982);

spellPut("bing", -19976);

spellPut("bo", -19805);

spellPut("bu", -19784);

spellPut("ca", -19775);

spellPut("cai", -19774);

spellPut("can", -19763);

spellPut("cang", -19756);

spellPut("cao", -19751);

spellPut("ce", -19746);

spellPut("ceng", -19741);

spellPut("cha", -19739);

spellPut("chai", -19728);

spellPut("chan", -19725);

spellPut("chang", -19715);

spellPut("chao", -19540);

spellPut("che", -19531);

spellPut("chen", -19525);

spellPut("cheng", -19515);

spellPut("chi", -19500);

spellPut("chong", -19484);

spellPut("chou", -19479);

spellPut("chu", -19467);

spellPut("chuai", -19289);

spellPut("chuan", -19288);

spellPut("chuang", -19281);

spellPut("chui", -19275);

spellPut("chun", -19270);

spellPut("chuo", -19263);

spellPut("ci", -19261);

spellPut("cong", -19249);

spellPut("cou", -19243);

spellPut("cu", -19242);

spellPut("cuan", -19238);

spellPut("cui", -19235);

spellPut("cun", -19227);

spellPut("cuo", -19224);

spellPut("da", -19218);

spellPut("dai", -19212);

spellPut("dan", -19038);

spellPut("dang", -19023);

spellPut("dao", -19018);

spellPut("de", -19006);

spellPut("deng", -19003);

spellPut("di", -18996);

spellPut("dian", -18977);

spellPut("diao", -18961);

spellPut("die", -18952);

spellPut("ding", -18783);

spellPut("diu", -18774);

spellPut("dong", -18773);

spellPut("dou", -18763);

spellPut("du", -18756);

spellPut("duan", -18741);

spellPut("dui", -18735);

spellPut("dun", -18731);

spellPut("duo", -18722);

spellPut("e", -18710);

spellPut("en", -18697);

spellPut("er", -18696);

spellPut("fa", -18526);

spellPut("fan", -18518);

spellPut("fang", -18501);

spellPut("fei", -18490);

spellPut("fen", -18478);

spellPut("feng", -18463);

spellPut("fo", -18448);

spellPut("fou", -18447);

spellPut("fu", -18446);

spellPut("ga", -18239);

spellPut("gai", -18237);

spellPut("gan", -18231);

spellPut("gang", -18220);

spellPut("gao", -18211);

spellPut("ge", -18201);

spellPut("gei", -18184);

spellPut("gen", -18183);

spellPut("geng", -18181);

spellPut("gong", -18012);

spellPut("gou", -17997);

spellPut("gu", -17988);

spellPut("gua", -17970);

spellPut("guai", -17964);

spellPut("guan", -17961);

spellPut("guang", -17950);

spellPut("gui", -17947);

spellPut("gun", -17931);

spellPut("guo", -17928);

spellPut("ha", -17922);

spellPut("hai", -17759);

spellPut("han", -17752);

spellPut("hang", -17733);

spellPut("hao", -17730);

spellPut("he", -17721);

spellPut("hei", -17703);

spellPut("hen", -17701);

spellPut("heng", -17697);

spellPut("hong", -17692);

spellPut("hou", -17683);

spellPut("hu", -17676);

spellPut("hua", -17496);

spellPut("huai", -17487);

spellPut("huan", -17482);

spellPut("huang", -17468);

spellPut("hui", -17454);

spellPut("hun", -17433);

spellPut("huo", -17427);

spellPut("ji", -17417);

spellPut("jia", -17202);

spellPut("jian", -17185);

spellPut("jiang", -16983);

spellPut("jiao", -16970);

spellPut("jie", -16942);

spellPut("jin", -16915);

spellPut("jing", -16733);

spellPut("jiong", -16708);

spellPut("jiu", -16706);

spellPut("ju", -16689);

spellPut("juan", -16664);

spellPut("jue", -16657);

spellPut("jun", -16647);

spellPut("ka", -16474);

spellPut("kai", -16470);

spellPut("kan", -16465);

spellPut("kang", -16459);

spellPut("kao", -16452);

spellPut("ke", -16448);

spellPut("ken", -16433);

spellPut("keng", -16429);

spellPut("kong", -16427);

spellPut("kou", -16423);

spellPut("ku", -16419);

spellPut("kua", -16412);

spellPut("kuai", -16407);

spellPut("kuan", -16403);

spellPut("kuang", -16401);

spellPut("kui", -16393);

spellPut("kun", -16220);

spellPut("kuo", -16216);

spellPut("la", -16212);

spellPut("lai", -16205);

spellPut("lan", -16202);

spellPut("lang", -16187);

spellPut("lao", -16180);

spellPut("le", -16171);

spellPut("lei", -16169);

spellPut("leng", -16158);

spellPut("li", -16155);

spellPut("lia", -15959);

spellPut("lian", -15958);

spellPut("liang", -15944);

spellPut("liao", -15933);

spellPut("lie", -15920);

spellPut("lin", -15915);

spellPut("ling", -15903);

spellPut("liu", -15889);

spellPut("long", -15878);

spellPut("lou", -15707);

spellPut("lu", -15701);

spellPut("lv", -15681);

spellPut("luan", -15667);

spellPut("lue", -15661);

spellPut("lun", -15659);

spellPut("luo", -15652);

spellPut("ma", -15640);

spellPut("mai", -15631);

spellPut("man", -15625);

spellPut("mang", -15454);

spellPut("mao", -15448);

spellPut("me", -15436);

spellPut("mei", -15435);

spellPut("men", -15419);

spellPut("meng", -15416);

spellPut("mi", -15408);

spellPut("mian", -15394);

spellPut("miao", -15385);

spellPut("mie", -15377);

spellPut("min", -15375);

spellPut("ming", -15369);

spellPut("miu", -15363);

spellPut("mo", -15362);

spellPut("mou", -15183);

spellPut("mu", -15180);

spellPut("na", -15165);

spellPut("nai", -15158);

spellPut("nan", -15153);

spellPut("nang", -15150);

spellPut("nao", -15149);

spellPut("ne", -15144);

spellPut("nei", -15143);

spellPut("nen", -15141);

spellPut("neng", -15140);

spellPut("ni", -15139);

spellPut("nian", -15128);

spellPut("niang", -15121);

spellPut("niao", -15119);

spellPut("nie", -15117);

spellPut("nin", -15110);

spellPut("ning", -15109);

spellPut("niu", -14941);

spellPut("nong", -14937);

spellPut("nu", -14933);

spellPut("nv", -14930);

spellPut("nuan", -14929);

spellPut("nue", -14928);

spellPut("nuo", -14926);

spellPut("o", -14922);

spellPut("ou", -14921);

spellPut("pa", -14914);

spellPut("pai", -14908);

spellPut("pan", -14902);

spellPut("pang", -14894);

spellPut("pao", -14889);

spellPut("pei", -14882);

spellPut("pen", -14873);

spellPut("peng", -14871);

spellPut("pi", -14857);

spellPut("pian", -14678);

spellPut("piao", -14674);

spellPut("pie", -14670);

spellPut("pin", -14668);

spellPut("ping", -14663);

spellPut("po", -14654);

spellPut("pu", -14645);

spellPut("qi", -14630);

spellPut("qia", -14594);

spellPut("qian", -14429);

java传递汉字翻译为英文_java写的能将汉字翻译成拼音的类相关推荐

  1. 汉字转化成拼音工具类介绍

    工具:pinyin4j pinyin4j是一个支持将简体和繁体中文转换到成拼音的Java开源类库 pinyin4j的官方下载地址:http://sourceforge.net/projects/pin ...

  2. Java io字符流读入英文_Java IO 系列教程(四)-字符输入流(2)

    本文介绍字符输入流 在前面一节中,我们向一个文件中写入了一些字符,通过图片可以看出总共是6个中文字符和一个换行,总共是20个字节,可以推算出字符编码是utf-8,每个汉子占3三个字节.本文就用字符输入 ...

  3. java统计string中文数字英文_Java学习(4):统计一个文件中的英文,中文,数字,其他字符以及字符总数...

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  4. java 去除字符串中的英文_Java利用正则表达式去掉字符串中的英文

    利用正则表达式去掉字符串中的英文String str = "111,aaa,222,bbb"; Pattern p = Pattern.compile("[a-zA-z] ...

  5. java判定输入是否为英文_java中如何判断输入的是英文还是中文

    字符 人们使用的记号,抽象意义上的一个符号. '1', '中', 'a', '$', '¥', -- 字节 计算机中存储数据的单元,一个8位的二进制数,是一个很具体的存储空间. 0x01, 0x45, ...

  6. java在容器中导入图片_Java程序员必学技术:@Import直接导入类,在容器@Configuration、@Component中是怎么直接导入类注册到容器的?...

    Java程序员必学技术:@Import直接导入类,在容器@Configuration.@Component中是怎么直接导入类注册到容器的? Spring IoC 容器是一个管理 Bean 的容器,在 ...

  7. java容器集合类的区别用法_Java容器笔记(二):不同集合实现类的特点与区别...

    package java.util包中的Collection相关接口和类如下图: Collection.png 仅讨论Java.util包中的常见集合类,不涉及java.util的子包concurre ...

  8. java log输出到文件路径_Java - 配置log4j的日志文件路径 (附-获取当前类路径的多种方法)...

    1 日志路径带来的痛点 Java 项目中少不了要和log4j等日志框架打交道, 开发环境和生产环境下日志文件的输出路径总是不一致, 设置为绝对路径的方式缺少了灵活性, 每次变更项目路径都要修改文件, ...

  9. java压缩zip文件夹错误_Java将文件或者文件夹压缩成zip(修复文件夹中存在多个文件报Stream Closed错误问题)...

    项目场景: Java将文件或者文件夹压缩成zip(修复文件夹中存在多个文件报Stream Closed错误问题) 问题描述: 最近的项目需要将多级文件夹压缩成zip,网上找了几个工具类,都会报错,所以 ...

最新文章

  1. SecureCRT中sqlplus,使用Backspace删除时 ^H^H
  2. 获取某个日期的当前周一的时间
  3. 现代密码学3.3--伪随机生成器/PRG
  4. SAP Spartacus B2B页面unit tree取数据的设计逻辑
  5. 1900页Python系列PPT分享四:字符串与正则表达式(109页)
  6. 23. WebVR播放器: 消费升级带来的机遇
  7. BZOJ 3744: Gty的妹子序列 [分块]
  8. paip.提升用户体验----c++ c# 配色方案
  9. 【代码优化】使用enum代替int常量
  10. H53D旋转-遁地龙卷风
  11. ResNet+ FPN网络结构
  12. RJ45网线水晶头线序,568A与568B区别,交叉线与直连线区别,10/100M base TX RJ45 接口引脚功能定义
  13. 生意参谋和数据银行盘点:品牌+市场+产品
  14. OpenNLP进行中文命名实体识别(上:预处理及训练模型)
  15. SQLServer简繁互换
  16. vue中当图片地址无效的时候,显示默认图片
  17. 利用Ajax爬取今日头条头像,街拍图片。关于崔庆才python爬虫爬取今日头条街拍内容遇到的问题的解决办法。
  18. 首师大附中科创教育平台 我的刷题记录 3120 LJX的校园:入学典礼
  19. 中国传统文化-雨课堂期末2022
  20. 美团动态线程池实践思路开源项目(DynamicTp),线程池源码解析及通知告警篇

热门文章

  1. 登录本地MySQL数据库服务器
  2. 5G融合通信网关(应急通讯、车载通讯、视频传输)
  3. C# 解决上传附件大小限制
  4. Python实验报告 实验15 - 体育竞技分析
  5. 使用KMS批量激活操作系统
  6. vscode 设置用户代码片段(怎么用命令式快速生成一段代码)
  7. Android利用zxing生成二维码,识别二维码,中间填充图片超详细、超简易教程
  8. iphone5s怎么取消iphone系统更新如何关闭系统更新提醒
  9. 原型设计工具——“墨刀”的介绍与基本教程
  10. [量化-034]金融哲学-道德经解读-005-“道”是什么