1. 、/* int -> byte[] */ 
  2. public static byte[] intToBytes(int num) {
  3. byte[] b = new byte[4]; 
  4. for (int i = 0; i < 4; i++) { 
  5. b[i] = (byte) (num >>> (24 - i * 8)); 
  6. }  
  7. return b; 
  8.  
     
    1. /* byte[]->int */
    2. public final static int getInt(byte[] buf, boolean asc) { 
    3. if (buf == null) { 
    4. throw new IllegalArgumentException("byte array is null!"); 
    5. if (buf.length > 4) {
    6. throw new IllegalArgumentException("byte array size > 4 !"); 
    7. int r = 0; 
    8. if (asc) 
    9. for (int i = buf.length - 1; i >= 0; i--) { 
    10. r <<= 8; 
    11. r |= (buf[i] & 0x000000ff);
    12. }
    13. else
    14. for (int i = 0; i < buf.length; i++) {
    15. r <<= 8;
    16. r |= (buf[i] & 0x000000ff);
    17. }
    18. return r;
    19. }