Last active
December 31, 2015 20:49
-
-
Save ffbit/8042378 to your computer and use it in GitHub Desktop.
int to bytes
This file contains 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
import org.junit.Test; | |
import java.util.Arrays; | |
public class IntToBitesTest { | |
@Test | |
public void intToBytes() throws Exception { | |
int n = 1234567890; | |
byte[] a = new byte[4]; | |
System.out.println(Integer.toHexString(n)); | |
for (int i = 0; i < a.length; i++) { | |
a[a.length - i - 1] = (byte) (n >> (8 * i) & 0xFF); | |
} | |
for (byte e : a) { | |
System.out.print(Integer.toHexString(e & 0xFF) + " "); | |
} | |
System.out.println(); | |
System.out.println(Arrays.toString(a)); | |
} | |
} |
This file contains 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
499602d2 | |
49 96 2 d2 | |
[73, -106, 2, -46] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment