Convert long to Bytes : Data Type cast « Data Type « Java






Convert long to Bytes

   


public class Util {
  public static byte[] toBytes(long n) {
    byte[] b = new byte[8];
    b[0] = (byte) (n >>> 56);
    b[1] = (byte) (n >>> 48);
    b[2] = (byte) (n >>> 40);
    b[3] = (byte) (n >>> 32);
    b[4] = (byte) (n >>> 24);
    b[5] = (byte) (n >>> 16);
    b[6] = (byte) (n >>> 8);
    b[7] = (byte) (n);

    return b;
  }
}

   
    
    
  








Related examples in the same category

1.Cast a float or double to an integral valueCast a float or double to an integral value
2.A simple casting demo
3.Casting Demo
4.Get the minimum and maximum value of a primitive data types?
5.Class with methods for type conversion
6.Data type conversion
7.Data type Conversion Util
8.Convert Byte array to Int
9.Convert Number To Target Class
10.Convert byte array to Long
11.Conversion utilities
12.Get Time From Date
13.Get String From Date
14.A method used to build a date for use Marker XML and KML data
15.A method used to build a date for display in line with existing formatting rules