怎么转?int是primitive,byte[]是non-primitive,不能相互转换!

解决方案 »

  1.   

    Integer.toBinaryString(int i)转换为2进制字符串
      

  2.   

    在不将int转成别的类型时,怎么将int转成byte[]
    没看明白这句?
    下面不符合要求吧?转换类型了public class bety { public static void main(String[] args) {
    int i;
    i=10;
    byte a=(byte)i;
    System.out.println(a);
    }
    }
      

  3.   

    int a = 111;
    byte b[] = Integer.toString(a).getBytes();
    直接转不知道,还是需要先转成String吧
      

  4.   

    /**
       *Convert an integer to bytes, and save it into the specified byte array from the specified position.
       *@param piNumber the integer to be converted.
       *@param piByteLength its value is from 1 to 4, represents how many bytes will be used in dest byte array to save the integer.
       *If this value is smaller than the actual size of the integer, the higher byte(s) will be truncated.
       *@param pbArDest the destination byte array to save the converted bytes
       *@param piStartPos from which position the bytes will be saved.
       *@return true if the convert procedure success, otherwise return false.
       *@see com.saili.util.DataConverter#bytesToint(byte pb0,byte pb1,byte pb2,byte pb3,int piByteLength)
       *@see com.saili.util.DataConverter#bytesToint(byte [] pbArSource,int piStartPos,int piByteLength)
       */
      public static boolean intTobytes(int piNumber,int piByteLength,byte[] pbArDest,int piStartPos)
      {
        if(pbArDest == null || piStartPos < 0 || piByteLength<1 || piByteLength>4 || pbArDest.length < piStartPos+piByteLength)
          return false;
        //converter here
        for(int i=0;i<piByteLength;i++){
          pbArDest[piStartPos+i] = (byte)((piNumber>> (i*8))& 0xff);
        }
        return true;
      }