waltermilner

Untitled

Nov 13th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1.     // put an int into a byte array
  2.    int n= 0x01020304; // easyto test if set out in hex
  3.    byte[] bytes = new byte[4];
  4.    bytes[3]=(byte) n; // lowest 8 bits into byte[3]
  5.    n>>=8; // this is n = n >> 8. The >> is a bit shift to the right 8 places
  6.    // so it loses the lower 8 bits, and shifts the rest along
  7.    bytes[2]=(byte) n; // this now gets what was the second lowest 8 bits
  8.     n>>=8;
  9.    bytes[1]=(byte) n; // this would be better as a loop
  10.     n>>=8;
  11.    bytes[0]=(byte) n;
  12.    for (int i=0; i<4; i++)
  13.      System.out.println(bytes[i]); // 1 2 3 4
Advertisement
Add Comment
Please, Sign In to add comment