int byteArrToInt(byte[] b, int off) {
       int value= 0;
       for (int i = 0; i < 4; i++) {
           int shift = (4 - 1 - i) * 8;
           value +=(b[i + off] & 0x000000FF) << shift;
       }
       return value;
 }

解决方案 »

  1.   

    ByteBuffer.wrap(null).getShort();
    注意你的byte是big-end还是little-end
      

  2.   


    我的二进制数据是存在 byte[] b 里,好像变量b没有wrap的方法?你的ByteBuffer是如何声明的?
      

  3.   

    /**
         * 获取byte数组的头两个字节对应的int值
         * @param bytes
         * @throws StringIndexOutOfBoundsException
         * @return
         */
        public static int byteArray2int(byte[] byteArray) throws StringIndexOutOfBoundsException {
            int num = byteArray[0] & 0xFF;  
            num |= ((byteArray[1] << 8) & 0xFF00);  
            return num;  
        }
        
        /**
         * 获取byte数组的头四个字节对应的int值(适用于低位在前,高位在后)
         * @param bytes
         * @throws StringIndexOutOfBoundsException
         * @return
         */
        public static int byteArray4intL(byte[] byteArray) throws StringIndexOutOfBoundsException {  
            int num = byteArray[0] & 0xFF;  
            num |= ((byteArray[1] << 8) & 0xFF00);  
            num |= ((byteArray[2] << 16) & 0xFF0000);  
            num |= ((byteArray[3] << 24) & 0xFF000000);  
            return num;  
        }
      

  4.   

    http://www.360doc.com/content/06/0809/10/8473_177064.shtml
      

  5.   

    java.nio.ByteBuffer.wrap(bytes).getShort();