package cn.vetech.framework.util;
/**
* 获取汉字首字母拼音类
* @author vesoft
*
*/
public class CnStr {
private char[] chartable = {
'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
'击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
'撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
};
private char[] alphatable = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private int[] table = new int[27];
//初始化
{
for (int i = 0; i < 27; ++i) {
table[i] = gbValue(chartable[i]);
}
}
//主函数,输入字符,得到他的声母,
//英文字母返回对应的大写字母
//其他非简体汉字返回 '0'
public char Char2Alpha(char ch) {
if (ch >= 'a' && ch <= 'z')
return (char) (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
return ch;
int gb = gbValue(ch);
if (gb < table[0])
return '0';
int i;
for (i = 0; i < 26; ++i) {
if (match(i, gb))
break;
}
if (i >= 26)
return '0';
else
return alphatable[i];
}
//根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
public String getAlpha(String SourceStr,int k) {
String Result = "";
int StrLength = SourceStr.length();
int i;
int kk=0;
if(StrLength >k )
kk = k;
else
kk = StrLength;
try {
for (i = 0; i < kk; i++) {
Result += Char2Alpha(SourceStr.charAt(i));
}
} catch (Exception e) {
Result = "";
}
return Result;
}
private boolean match(int i, int gb) {
if (gb < table[i])
return false;
int j = i + 1;
//字母Z使用了两个标签
while (j < 26 && (table[j] == table[i]))
++j;
if (j == 26)
return gb <= table[j];
else
return gb < table[j];
}
//取出汉字的编码
private int gbValue(char ch) {
String str = new String();
str += ch;
try {
byte[] bytes = str.getBytes("GB2312");
if (bytes.length < 2)
return 0;
return (bytes[0] << 8 & 0xff00) + (bytes[1] &
0xff);
} catch (Exception e) {
return 0;
}
}
/**
* 字符转换 转换到国标简体
*/
public String toGB(String strIn) {
String strOut = null;
if (strIn == null || (strIn.trim()).equals("")) {
return "";
}
try {
strOut=new String(strIn.getBytes("GBK"),"GBK");
if(strOut.endsWith(strIn)){
return strIn;
}
strOut = new String(strIn.getBytes("ISO-8859-1"), "GBK");
return (strOut);
} catch (Exception e) {
return "";
}
}// end of UnicodeToGB()
/**
* 获得字符串的ISO8859_1的字节数组
*
* @param s
* @return
*/
public byte[] getISO8859_1(String s) {
if (s == null) {
return null;
} else {
try {
return s.getBytes("ISO8859_1");
} catch (Exception e) {
return null;
}
}
}
// 用于处理页内生成的中文数据在写入数据库时的处理,由GBK变为iso8859-1
public String toISO(String s_string) {
try {
String des = new String(s_string.getBytes("GB2312"), "iso8859-1");
return des;
} catch (Exception ex) {
String des = "";
return des;
}
}
// 将中英文字串转换成纯英文字串
public String toACSII(String sss) {
StringBuffer sb = new StringBuffer();
byte[] bt = sss.getBytes();
for (int i = 0; i < bt.length; i++) {
if (bt[i] < 0) {
int yy = bt[i];
// 是汉字去高位1
sb.append((char) (yy & 0x7f));
} else {// 是英文字符 补0作记录
sb.append((char) 0);
sb.append((char) bt[i]);
}
}
return sb.toString();
}
// 将经转换的字串还原
public String toCH(String ss) {
byte[] bt = ss.getBytes();
int i, l = 0;
int length = bt.length, j = 0;
for (i = 0; i < length; i++) {
if (bt[i] == 0) {
l++;
}
}
byte[] bt2 = new byte[length - l];
for (i = 0; i < length; i++) {
if (bt[i] == 0) {
i++;
bt2[j] = bt[i];
} else {
bt2[j] = (byte) (bt[i] | 0x80);
}
j++;
}
String tt = new String(bt2);
return tt;
}
/**
* 转换到unicode编码
*/
public String toGB2312(String strIn1) {
String strOut1 = null;
if (strIn1 == null || (strIn1.trim()).equals("")) {
return "";
}
try {
strOut1 = new String(strIn1.trim().getBytes("GB2312"));
return (strOut1);
} catch (Exception e) {
return "";
}
}// end of ToUnicode()
/**
* 转换到BIG
*/
public String toBIG(String strIn2) {
String strOut2 = null;
if (strIn2 == null || (strIn2.trim()).equals("")) {
return null;
}
try {
strOut2 = new String(strIn2.getBytes("GBK"), "BIG5");
return (strOut2);
} catch (Exception e) {
return null;
}
}// end of BIG
/***************************************************************************
* 检验一个字符串是否是空值
**************************************************************************/
public static boolean isEmpty(String str) {
if (null == str || ("").equals(str))
return true;
else
return false;
}
public String Lock(String strsource) {
String outstr = "";
long a, a1 = 0, a2 = 0, tmp1, tmp2, tt;
String min = "95160";
strsource = toACSII(strsource);
byte[] tmp = strsource.getBytes();
for (int k = 0; k < strsource.length(); k++) {
a = tmp[k];
a1 = a / 4;
a1 = a1 % 16;
tmp1 = a / 64;
tmp2 = a % 4;
a2 = tmp1 * 4 + tmp2;
tt = (Long.parseLong(min) + k) % 2;
if (tt == 1)
a1 = a1 + 16;
else
a2 = a2 + 16;
char u1 = (char) (a1 + 33);
char r1 = (char) (a2 + 61);
outstr = outstr + u1 + r1;
}
return outstr;
}
public String unLock(String strsource) {
long a1 = 0, a2 = 0, tmp1, tmp2;
String outstr = "";
byte[] tmp = strsource.getBytes();
for (int k = 0; k < strsource.length() / 2; k++) {
a1 = tmp[k * 2];
a2 = tmp[k * 2 + 1];
a1 = a1 - 33;
a2 = a2 - 61;
if (a1 >= 16)
a1 = a1 - 16;
else
a2 = a2 - 16;
tmp1 = a2 / 4;
tmp2 = a2 % 4;
a2 = tmp1 * 64 + tmp2;
char u1 = (char) (a1 * 4 + a2);
outstr = outstr + u1;
}
return toCH(outstr);
}
public static void main(String[] arg){
//CnStr cs = new CnStr();
try{
//System.out.println(System.getProperty("file.separator")) ;
//System.out.println(System.getProperty("user.dir")) ;
//System.out.println(System.getProperty("path.separator")) ;
}catch(Exception es){
//System.out.println("error" + es.toString()) ;
}
}
}