考虑是不是可以先转成int(或其他),再转BCD?

解决方案 »

  1.   

    我能看懂代码,只要结果,大侠们帮帮忙
    BCD码指8421码
      

  2.   

    另外第1题本来就是int,进制不同不影响,方法大概是 byte[] int2Bytes(int hex){}
    调用时  =int2Bytes(0x16)即同 int2Bytes(32);
      

  3.   

    //4字节,2和8的自己搞定,同理
    public static byte[] IntToByteArray(int i) {
      byte[] result = new byte[4];
    result[0] = (byte)(i & 0xFF);
    result[1] = (byte)((i >> 8) & 0xFF);
    result[2] = (byte)((i >> 16) & 0xFF);
    result[3] = (byte)((i >> 24) & 0xFF);
    return result;
    }
      

  4.   

    在“字节数组<===>数值”中的数值到底是(由1.字节数值: 数据由4个字节组成),
    还是由(2.BCD数值:数据由多个字节组成)?
      

  5.   

    上面我写的回复一个打错"=int2Bytes(0x16)即同 int2Bytes(32);"
    应该为"=int2Bytes(0x16)即同 int2Bytes(22);"其实1的问题很易搞掂,不过就是概念"排列由低位字节到高位字节"不太明,
    是否如: whywzf(古风) 说的还是把其反过来?对于: xwlovesh(xiewei) 的提问那个数值分别是1与2的请大侠继续搞掂2, thanks
      

  6.   

    关于2:
    比如:十进制数 -325.25
    变为字节数组就是:
    [-3][25][.2][5 ]

    [B3][25][A2][5F]所占的字节数由具体的数字应该占多少就多少
      

  7.   

    近来我脑子总是不灵通,很小的问题都混淆了,
    笨笨的问一下whywzf(古风) :
    关于高字节低字节,字节的高四位及低四位的概念常混淆,请解答一下对一个字节: 1111 0000 那里是高四位?
    对多个字节如: [0][1][2][3]究竟是[0]还是[3]是高位字节?
      

  8.   

    1111(高4位)
    对多字节,不一定[0]是高位,还是[3]是高位
    在我写的方法中,是定义【0】是低字节的,比如i=1,那么[01][00][00][00],所以说是由低到高的,根据开发者的习惯或者某些性能上可以造成便利的原因,低--->高;高--->低两种情况都存在。比如汉字的双字节是高到低的,音频的是低到高的;支持intelCPU的是低到高的,其他CPU也有高到低的……
      

  9.   

    初步搞定,熟悉的帮忙看看是否正确
    //1.-----------------------------------------------------------------------
      public static byte[] hex2Bytes(int hex, int size) {
        byte[] result = new byte[size];
        for(int i=0;i<size;i++){
          result[i] = (byte) ( ( ( (long) hex) >> (8 * i)) & 0xFF);
        }
        return result;
      }
      public static int byte2Hex(byte[] packetBytes) {
        int result = 0;
        if (packetBytes == null) {
          return result;
        }
        for (int i = 0; i < packetBytes.length; i++) {
          result += (packetBytes[i] & 0xff) << (i * 8);
        }
        return result;
      }
    //2.-----------------------------------------------------------------------
      public static byte[] double2BCD(double d) {
        byte[] byteReturn = null;
        int cycle = 0;
        boolean needSuffix = false;
        String s = Double.toString(d);
        int len = s.length();
        if (len % 2 == 0) {
          needSuffix = false;
          len = len / 2;
          cycle = len;
        }
        else {
          needSuffix = true;
          len = (len + 1) / 2;
          cycle = len - 1;
        }
        byteReturn = new byte[len];
        int int1 = 0, int2 = 0;    for (int i = 0; i < cycle; i++) {
          String temp1 = s.substring(2 * i, 2 * i + 1);
          String temp2 = s.substring(2 * i + 1, 2 * i + 2);
          if (temp1.equals("-")) {
            int1 = 0xB;
          }
          else if (temp1.equals(".")) {
            int1 = 0xA;
          }
          else {
            int1 = Integer.parseInt(temp1);
          }
          int1 = int1 * 16;      if (temp2.equals("-")) {
            int2 = 0xB;
          }
          else if (temp2.equals(".")) {
            int2 = 0xA;
          }
          else {
            int2 = Integer.parseInt(temp2);
          }
          byteReturn[i] = (byte) ( (int1 + int2) & 0xFF);
        }
        //补位0xF
        if (needSuffix) {
          int suffix = Integer.parseInt(s.substring(2 * len - 2, 2 * len - 1));
          byteReturn[len - 1] = (byte) (suffix * 16 + 0xF);
        }
        return byteReturn;
      }  public static double byteBCD2Double(byte[] packetBytes) {
        if (packetBytes == null) {
          return 0;
        }
        int length = packetBytes.length;
        byte b = 0;
        String stringValue = "";
        boolean hasMinus = false, hasDot = false;
        for (int i = 0; i < length; i++) {
          String temp = Integer.toHexString(packetBytes[i] & 0xFF);
          if(temp!=null && temp.length()<2){
            temp="0"+temp;
          }
          stringValue = stringValue + temp;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < stringValue.length(); i++) {
          char c = stringValue.toUpperCase().charAt(i);
          if (c == 'B') {
            c = '-';
            sb.append(c);
          }
          else if (c == 'A') {
            c = '.';
            sb.append(c);
          }
          else if (c != 'F') {
            sb.append(c);
          }
        }
        stringValue = sb.toString();
        return Double.valueOf(stringValue).doubleValue();
      }