Created
          July 28, 2015 06:31 
        
      - 
      
- 
        Save nulpatrol/29074dafb1891e814779 to your computer and use it in GitHub Desktop. 
    Pack and unpack IP address (InetAddress) to/from int
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public static int pack(byte[] bytes) { | |
| int val = 0; | |
| for (byte oneByte : bytes) { | |
| val <<= 8; | |
| val |= oneByte & 0xff; | |
| } | |
| return val; | |
| } | |
| public static short[] unpack(int bytes) { | |
| return new short[] { | |
| (short)((bytes >>> 24) & 0xff), | |
| (short)((bytes >>> 16) & 0xff), | |
| (short)((bytes >>> 8) & 0xff), | |
| (short)((bytes ) & 0xff) | |
| }; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment