JAVA版StarDict星际译王简单实现(三)
符串
*
* @param utf_data
* byte[] - UTF-8编码字节数组
* @param len
* int - 字节数组长度
* @return String - 变换后的Unicode编码字符串
*/
public static String UTF2Uni(byte[] utf_data, int len)
{
StringBuffer unis = new StringBuffer();
char unic = 0;
int ptr = 0;
int cntBits = 0;
for (; ptr < len;)
{
cntBits = getCntBits(utf_data[ptr]);
if (cntBits == -1)
{
++ptr;
continue;
} else if (cntBits == 0)
{
unic = UTFC2UniC(utf_data, ptr, cntBits);
++ptr;
} else
{
unic = UTFC2UniC(utf_data, ptr, cntBits);
ptr += cntBits;
}
unis.append(unic);
}
return unis.toString();
}
/**
* 将指定的UTF-8字节组合成一个Unicode编码字符
* @param utf byte[] - UTF-8字节数组
* @param sptr int - 编码字节起始位置
* @param cntBits int - 编码字节数
* @return char - 变换后的Unicode字符
*/
public static char UTFC2UniC(byte[] utf, int sptr, int cntBits)
{
/*
* Unicode <-> UTF-8 U-00000000 - U-0000007F: 0xxxxxxx U-00000080 -
* U-000007FF: 110xxxxx 10xxxxxx U-00000800 - U-0000FFFF: 1110xxxx
* 10xxxxxx 10xxxxxx U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx
* 10xxxxxx U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx
* 10xxxxxx U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx
* 10xxxxxx 10xxxxxx
*/
int uniC = 0; // represent the unicode char
byte firstByte = utf[sptr];
int ptr = 0; // pointer 0 ~ 15
// resolve single byte UTF-8 encoding char
if (cntBits == 0)
return (char) firstByte;
// resolve the first byte
firstByte &= (1 << (7 - cntBits)) - 1;
// resolve multiple bytes UTF-8 encoding char(except the first byte)
for (int i = sptr + cntBits - 1; i > sptr; --i)
{
byte utfb = utf[i];
uniC |= (utfb & 0x3f) << ptr;
ptr += 6;
}
uniC |= firstByte << ptr;
return (char) uniC;
}
/**
* 根据给定字节计算UTF-8编码的一个字符所占字节数,UTF-8规则定义,字节标记只能为0或2~6
* @param b
* @return
*/
private static int getCntBits(byte b)
{
int cnt = 0;
if (b == 0)
return -1;
for (int i = 7; i >= 0; --i)
{
if (((b >> i) & 0x1) == 1)
++cnt;
else
break;
}
return (cnt > 6 || cnt == 1) -1 : cnt;
}
/**
* 显示data内容
* @param data_buf UTF-8的单词释义数组
* @param data_length UTF-8的单词释义数组长度
*/
public static void display_data(byte[] data_buf, int data_length[])
{
// 将UTF-8byte字节数组转为当前环境字符并显示
// String tempString = UTF8Decode(data_buf, 0, data_length[0]);
String tempString = UTF2Uni(data_buf, data_length[0]);
// String tempString = new String(data_buf);
data_buf = null;
System.out.println(tempString);
}
/**
* 从idx文件中搜索由word指定的单词,并保存相应的偏移和长度信息
* @param word
* @param data_poffset
* @param data_plength
* @return 是否搜索成功
*/
public static boolean search_word(String word, int[] data_poffset,
int[] data_plength)
{
String wd[] = new String[1];
boolean temp = false;
int len[] = new int[1];
// 从idx文件中获取当前目标单词
// for (get_word(wd, data_poffset, data_plength); end; get_word(wd,
// data_poffset, data_plength))
// {
while (get_word(wd, data_poffset, data_plength, len))
{
// System.out.println("compared_word:"+wd[0]);
// if (wd[0].compareToIgnoreCase(word) == 0) //
// 比较字符串s1和s2,但不区分字母的大小写
if (strsEqualsIgnoreCase(wd[0], word) == 0)
{
System.out.println("compared_word:" + word + " " + wd[0]);
temp = true;
break;
}
}
return temp;
}
/**
* 从标准