Created
July 6, 2013 02:22
-
-
Save lyaotian/5938376 to your computer and use it in GitHub Desktop.
byte array vs 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 byteArrayToInt(byte[] b) { | |
| return b[3] & 0xFF | | |
| (b[2] & 0xFF) << 8 | | |
| (b[1] & 0xFF) << 16 | | |
| (b[0] & 0xFF) << 24; | |
| } | |
| public static byte[] intToByteArray(int a){ | |
| return new byte[] { | |
| (byte) ((a >> 24) & 0xFF), | |
| (byte) ((a >> 16) & 0xFF), | |
| (byte) ((a >> 8) & 0xFF), | |
| (byte) (a & 0xFF) | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment