获取汉字的拼音码(工具类)

项目正好用到要获取汉字的拼音码,从网上搜索到的一个工具类。


[java]   view plain copy
  1. package com.util;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. public class PinyinUtil {  
  6.   
  7.     static final int GB_SP_DIFF = 160;  
  8.     // 存放国标一级汉字不同读音的起始区位码  
  9.     static final int[] secPosValueList = { 160116371833207822742302,  
  10.             24332594278731063212347236353722373038584027,  
  11.             4086439045584684492552495600 };  
  12.     // 存放国标一级汉字不同读音的起始区位码对应读音  
  13.     static final char[] firstLetter = { 'a''b''c''d''e''f''g''h',  
  14.             'j''k''l''m''n''o''p''q''r''s''t''w''x',  
  15.             'y''z' };  
  16.   
  17.     /** 
  18.      * 获取一个字符串的拼音码 
  19.      * @param oriStr 
  20.      * @return 
  21.      */  
  22.     public static String getFirstLetter(String oriStr) {  
  23.         String str = oriStr.toLowerCase();  
  24.         StringBuffer buffer = new StringBuffer();  
  25.         char ch;  
  26.         char[] temp;  
  27.         for (int i = 0; i < str.length(); i++) { // 依次处理str中每个字符  
  28.             ch = str.charAt(i);  
  29.             temp = new char[] { ch };  
  30.             byte[] uniCode = new String(temp).getBytes();  
  31.             if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字  
  32.                 buffer.append(temp);  
  33.             } else {  
  34.                 buffer.append(convert(uniCode));  
  35.             }  
  36.         }  
  37.         return buffer.toString();  
  38.     }  
  39.   
  40.     /** 
  41.      * 获取一个汉字的拼音码 
  42.      * @param ch 
  43.      * @return 
  44.      */  
  45.     public static Character getFirstLetter(char ch) {  
  46.         // 过滤英文字母  
  47.         if((ch >= 'a' && ch <= 'z')) {  
  48.             return ch;  
  49.         } else if (ch >= 'A' && ch <= 'Z') {  
  50.             return (char) (ch + 32);  
  51.         }  
  52.           
  53.         byte[] uniCode = null;  
  54.         try {  
  55.             uniCode = String.valueOf(ch).getBytes("GBK");  
  56.         } catch (UnsupportedEncodingException e) {  
  57.             e.printStackTrace();  
  58.             return null;  
  59.         }  
  60.         if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字  
  61.             return '#';  
  62.         } else {  
  63.             return convert(uniCode);  
  64.         }  
  65.     }  
  66.   
  67.     /** 
  68.      * 获取一个汉字的拼音首字母。 GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码 
  69.      * 例如汉字“你”的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43 
  70.      * 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n’ 
  71.      */  
  72.     static char convert(byte[] bytes) {  
  73.         char result = '-';  
  74.         int secPosValue = 0;  
  75.         int i;  
  76.         for (i = 0; i < bytes.length; i++) {  
  77.             bytes[i] -= GB_SP_DIFF;  
  78.         }  
  79.         secPosValue = bytes[0] * 100 + bytes[1];  
  80.         for (i = 0; i < 23; i++) {  
  81.             if (secPosValue >= secPosValueList[i]  
  82.                     && secPosValue < secPosValueList[i + 1]) {  
  83.                 result = firstLetter[i];  
  84.                 break;  
  85.             }  
  86.         }  
  87.         return result;  
  88.     }  
  89.   
  90. }  


注:由于作者不是是谁,所以此文没注名出处!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值