undefined
发贴: 22
积分: 3
|
于 2005-05-12 19:59
public class Attributes { private byte type; private byte length; private byte[] value = null; //不定长
public Attributes(int typ, String value) { this.type = (byte) typ;
this.value = value.getBytes(); }
int getlengths() { return value.length + 2; }
byte[] tobyte() { byte[] newbyte = new byte[this.getlengths()]; newbyte[0] = this.type; newbyte[1] = this.length; for (int i = 0; i < this.getlengths() - 2; i++) { //System.out.println(this.value[i]); newbyte[i + 2] = this.value[i]; } return newbyte; } public static void main(String [] args) { Attributes att= new Attributes(5,"abcd"); System.out.println(att.tobyte()); } }
public class DAFrame { private byte code; private byte identifier; private byte length[] = new byte[2]; private byte Authenticator[] = new byte[16]; private Attributes attributes;
public DAFrame(int code, int i, byte[] authen, Attributes attri) { this.code = (byte) code; this.identifier = (byte) i; this.Authenticator = authen; this.attributes = attri; this.setlength(); }
void setlength() { //System.out.println("$$$$$$$$$$$$$$"); //System.out.println((byte) this.getlength()); this.length[0] = (byte) this.getlength(); int length1 = this.getlength(); length1 = length1 / 256 ; this.length[1] = (byte) length1; }
int getlength() { //System.out.println(new Attributes(5,"abc").getlengths()); return 20 + this.attributes.getlengths(); }
byte[] tobyte() { byte[] newbyte = new byte[this.getlength()]; newbyte[0] = this.code; newbyte[1] = this.identifier; newbyte[2] = this.length[0]; newbyte[3] = this.length[1]; for (int i = 0; i < this.Authenticator.length; i++) { newbyte[i + 4] = this.Authenticator[i]; } //错误处 byte[] attribyte = this.attributes.tobyte(); for (int i = 0; i < attribyte.length; i++) { newbyte[i + 20] = attribyte[i]; } return newbyte; } public static void main(String [] args) { byte [] b= new byte[3]; b[0]='a'; b[1]='b'; b[2]='c'; DAFrame daf = new DAFrame(5,12,b,new Attributes(22,"abc")); //daf.getlength(); byte[] by=daf.tobyte(); for(int i=0;i<by.length;i++) { System.out.print(by[i]+" * "); } //Attributes att= new Attributes(5,"abc"); //System.out.println(att.getlengths()); }
}
我给调好了,输出结果:
5 * 12 * 25 * 0 * 97 * 98 * 99 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 22 * 0 * 97 * 98 * 99 *
|