日期:2014-05-20  浏览次数:23582 次

求帮助,搜狗网申在线测试题。
 
2011-09-12 17:09 0人阅读 评论(0) 收藏 编辑 删除
首先说明本题目的原意:以下程序是GBK编码和解码程序,根据编码过程encode(),将解码过程decode()补充完整。写出输出结果,输出结果是一句话。

public class Test { 
public static void encode(byte[] in, byte[] out, int password) 

int len = in.length; 


int seed = password ^ 0x2c8f7672; 
for (int i = 0 ; i < len; ++i) { 
byte a = (byte)( ( in[i] ^ seed ) >>> 2 ); 
byte b = (byte)( ( ( ((int)in[i]) << 10 ) ^ seed ) >>> (10-6) ); 
a &= 0x3f; 
b &= 0xc0; 
out[i] = (byte)(a | b); 
seed = (((seed << 7) ^ seed ^ out[i]) + 536513); 






public static void decode(byte[] in, byte[] out, int password) 

int len = in.length; 


int seed = password ^ 0x2c8f7672; 
for (int i = 0 ; i < len; ++i) { 
//……


public static void main(String [] args) throws Exception 

int password = 0xa15ab37a; 
byte[] buf1 = {-80, -86, -85, 77, 23, -94, 2, 77, 111, -35, -60, 68, -62, -128, -113, 27, 84, 11, 0, 61, 13, -43, 56, -50, 39, 55, -99, 114, -28, 104, -65, 91, 66, -97, 52, 80, -109, -6, 11, 29, -14, 98, -16, -95, 38, 37, 122, -75, 72, -54, -56, -86, -112, -28, }; 
byte[] buf2 = new byte[buf1.length]; 
decode(buf1, buf2, password); 
System.out.println(new String(buf2, "GBK")); 



在//……填写适当的代码,然后运行,打印出结果。

------解决方案--------------------
Java code
public static void decode(byte[] in, byte[] out, int password)  
{  
int len = in.length;  


int seed = password ^ 0x2c8f7672;  
for (int i = 0 ; i < len; ++i) {  
//……
byte a = (byte)( (in[i] << 2)^ seed) ;  
byte b = (byte)( ( ( ((int)in[i]) >>> 4 ) ^ seed ) << 10 );  
a &= 252;  
b &= 3;  
out[i] = (byte)(a | b);  
seed = (((seed << 7) ^ seed ^ in[i]) + 536513);  
}
}

------解决方案--------------------
今天一天,相似的问题回答了N遍,看来要出专题贴了

http://topic.csdn.net/u/20110914/13/6d405601-b8b8-4b1f-bb9b-2c1f1e898c29.html

Java code
public static void decode(byte[] in, byte[] out, int password) {  
    int len = in.length;  

    int seed = password ^ 0x2c8f7672;  
    for (int i = 0 ; i < len; ++i) {  
        byte a = (byte)(in[i] & 0x3f);
        byte b = (byte)(in[i] & 0xc0);
        a = (byte)(((a << 2) ^ seed) & 0xfc);
        b = (byte)((((((int)b) << (10-6)) ^ seed) >> 10) & 0x03);
        out[i] = (byte)(a | b);
        byte a = (byte)( ( in[i] ^ seed ) >>> 2 );  
        seed = (((seed << 7) ^ seed ^ in[i]) + 536513);  
    }  
}