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. } 
    6. if (buf.length > 4) {
    7. throw new IllegalArgumentException("byte array size > 4 !"); 
    8. } 
    9. int r = 0; 
    10. if (asc) 
    11. for (int i = buf.length - 1; i >= 0; i--) { 
    12. r <<= 8; 
    13. r |= (buf[i] & 0x000000ff);
    14. }
    15. else
    16. for (int i = 0; i < buf.length; i++) {
    17. r <<= 8;
    18. r |= (buf[i] & 0x000000ff);
    19. }
    20. return r;
    21. }