Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:求教高手啊(ArrayIndexOutOfBoundsException) [Re:jfs771]
undefined





发贴: 22
积分: 3
于 2005-05-12 19:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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 *




话题树型展开
人气 标题 作者 字数 发贴时间
9196 求教高手啊(ArrayIndexOutOfBoundsException) jfs771 1979 2005-04-19 14:59
7438 Re:求教高手啊。。。。。 bluepure 323 2005-04-19 17:30
7498 Re:求教高手啊。。。。。 jfs771 221 2005-04-19 19:14
7374 Re:求教高手啊(ArrayIndexOutOfBoundsException) xiaoshitou 93 2005-05-11 15:56
7962 Re:求教高手啊(ArrayIndexOutOfBoundsException) undefined 2518 2005-05-12 19:59
9439 Re:求教高手啊(ArrayIndexOutOfBoundsException) undefined 571 2005-05-12 20:10
7978 Re:求教高手啊(ArrayIndexOutOfBoundsException) jfs771 31 2005-05-18 10:37

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923