当前位置:编程学习 > VB >>

这段JAVA代码怎么转换成VB代码


有没有高手能不能把下列代码研究下, 转换成VB 程序, 不懂JAVA 啊。
本人现在针对TAOBAO编写一个搜索程序, 不知道他的搜索关键字的编码规则, 下面是在网上找到一段JAVA代码。
但是不懂JAVA啊。。
有高手的话, 帮忙转化下, 关键部分即可!


编码:
Java代码 
/** 

 * (编码/加密)字节数组 
 *  
 * @author gembler 
 * @version 2008-12-3 下午03:14:43 
 *  
 * @param keyBytes 
 *            需要(编码/加密)的字节数组 
 *  
 * @return (编码/加密)后的字符串 
 */  
private static String encode(byte[] keyBytes) {  
  
    if (keyBytes == null || keyBytes.length < 1) {  
  
        return "";  
  
    }  
  
    /* 
     * 合并二进制码,  
     * 如:  
     *     00101010 11010011 00101101 10100011  
     *   to 
     *     00101010110100110010110110100011 
     */  
  
    StringBuilder mergrd = new StringBuilder();  
  
    for (int i = 0; i < keyBytes.length; i++) {  
  
        FormatUtil.formatBinary(keyBytes[i], mergrd);  
  
    }  
  
    /* 
     * 以5个bit为单位,计算能分多少组, 
     * 如: 
     *     00101010110100110010110110100011 
     *   to 
     *     00101 01011 01001 10010 11011 01000 11 
     *                                          | 
     *                                   (这个11为余下的位) 
     */  
  
    int groupCount = mergrd.length() / FIVE_BIT;  
  
    // 计算余下的位数  
    int lastCount = mergrd.length() % FIVE_BIT;  
  
    // 类似数据分页的算法,有余数的情况下需要加 1。  
    if (lastCount > 0) {  
  
        groupCount += 1;  
  
    }  
  
    /* 
     * (编码/加密) 
     */  
  
    StringBuilder sbEncoded = new StringBuilder();  
  
    // 循环所需的条件  
    int forMax = groupCount * FIVE_BIT;  
  
    // 每次递增5位来截取  
    for (int i = 0; i < forMax; i += FIVE_BIT) {  
  
        // 结束点  
        int end = i + FIVE_BIT;  
  
        /* 
         * 如果结束点比已合并的二进制码串的长度要大, 
         * 相当于有余数, 
         * 并且表示当前循环到了(已合并的二进制码串的长度 % FIVE_BIT)的那一截。 
         */  
  
        // 标记是否到了余数的那一截  
        boolean flag = false;  
  
        if (end > mergrd.length()) {  
  
            /* 
             * 如果结束点比已合并的二进制码串的长度要大, 
             * 结束点需要被重设为:  
             * 已合并的二进制码串的长度,等价于(i + lastCount). 并且重设标记。 
             */  
  
            end = (i + lastCount);  
  
            flag = true;  
  
        }  
  
        // 截取  
        String strFiveBit = mergrd.substring(i, end);  
  
        // 截取后从二进制转为十进制  
        int intFiveBit = Integer.parseInt(strFiveBit, BINARY);  
  
        if (flag) {  
  
            /* 
             * 如果结束点比已合并的二进制码串的长度要大, 
             * 或者是到了余数的那一截:  
             * 需要左移操作,假设余下的二进制位为:11, 
             * 那么需要从后面补0,左移操作后为 (000)11(000) 
             */  
  
            intFiveBit <<= (FIVE_BIT - lastCount);  
  
        }  
  
        // 利用该十进制数作为码表的索引获取对应的字符,并追加到sbEncoded  
        sbEncoded.append(CODEC_TABLE.charAt(intFiveBit));  
  
    }  
  
    return sbEncoded.toString();  
  

 
解码: 

Java代码 
/** 
 * (解码/解密)字符串 
 *  
 * @author gembler 
 * @version 2008-12-3 下午03:15:00 
 *  
 * @param code 
 *            需要(解码/解密)的字符串 
 * @param characterSet 
 *            字符集 
 *  
 * @return (解码/解密)后的字符串 
 */  
public static String decode(String code, String characterSet) {  
  
    if (code == null || code.length() < 1) {  
  
        return "";  
  
    }  
  
    /* 
     * 拆除每一个字符,从码表里获取相应的索引。 
     */  
  
    StringBuilder sbBinarys = new StringBuilder();  
  
    for (int i = 0; i < code.length(); i++) {  
  
        // 从码表里获取相应的索引  
        int index = getCodecTableIndex(code.charAt(i));  
  
        // 将十进制的索引转换为二进制串  
        String indexBinary = Integer.toBinaryString(index);  
  
        // 去掉前3个0,并且追加到sbBinarys  
        FormatUtil.formatBinary(indexBinary, sbBinarys, FIVE_BIT);  
  
    }  
  
    /* 
     * 按8个bit拆分,剩下的余数扔掉。 
     * 扔掉的余数是在(编码/加密)的分割时候,在分剩的余数的后面补的0 
     */  
  
    byte[] binarys = new byte[sbBinarys.length() / EIGHT_BIT];  
  
    for (int i = 0, j = 0; i < binarys.length; i++) {  
  
        // 每8个bit截取一份  
        String sub = sbBinarys.substring(j, j += EIGHT_BIT);  
  
        // 将截取下来的二进制串转换为十进制  
        Integer intBinary = Integer.valueOf(sub, BINARY);  
  
        binarys[i] = intBinary.byteValue();  
  
    }  
  
    String decoded = null;  
  
    if (characterSet == null || characterSet.length() < 1) {  
  
        // 采用默认语言环境的 character set。  
        decoded = new String(binarys);  
  
    } else {  
  
        try {  
  
            // 采用指定的 character set。  
            return new String(binarys, characterSet);  
  
        } catch (UnsupportedEncodingException e) {  
            // ignore...  
        }  
    }  
    return decoded;  
}  

测试: 

Java代码 
BufferedReader br = new BufferedReader(new InputStreamReader(  
        System.in));  
  
while (true) {  
  
    System.out.print("输入字符号串:");  
  
    String in = br.readLine();  
  
    if ("exit".equalsIgnoreCase(in)) {  
        break;  
    }  
  
    String enCode = Codec.encode(in);  
  
    String deCode = Codec.decode(enCode);  
    System.out.println();  
    System.out.println("original: " + in);  
    System.out.println("encode: " + enCode);  
    System.out.println("decode: " + deCode);  
    System.out.println();  
}  

--------------------编程问答-------------------- 不算太难嘛,就是几个转换函数需要自己写,比如字符串到byte数组,byte数组按5bit划分。
--------------------编程问答-------------------- 幫樓主頂一下 --------------------编程问答-------------------- 你可以找个 Base64 的 VB 程序,改成 Base32 方式,还有尾部的处理稍有不同 --------------------编程问答-------------------- 我看了BASE64的, 很长啊, 很复杂。 我看了这段JAVA, 应当是没有那么复杂的。 --------------------编程问答-------------------- 我看了BASE64的, 很长啊, 很复杂。 我看了这段JAVA, 应当是没有那么复杂的。
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,