Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. public class BitConverter
  2. {
  3.     public static byte[] getBytes(boolean x)
  4.     {
  5.         return new byte[]{
  6.             (byte) (x ? 1:0)
  7.                 };
  8.     }
  9.    
  10.     public static byte[] getBytes(char x)
  11.     {
  12.         return new byte[] {
  13.             (byte)x
  14.                 };
  15.     }
  16.    
  17.     public static byte[] getBytes(double x)
  18.     {
  19.         return getBytes(
  20.             Double.doubleToRawLongBits(x));
  21.     }
  22.    
  23.     public static byte[] getBytes(short x)
  24.     {
  25.         return new byte[] {
  26.             (byte)(x >>> 8),
  27.             (byte)x
  28.                 };
  29.     }
  30.    
  31.     public static byte[] getBytes(int x)
  32.     {
  33.         return new byte[] {
  34.             (byte)(x >>> 24),
  35.             (byte)(x >>> 16),
  36.             (byte)(x >>> 8),
  37.             (byte)x
  38.                 };
  39.     }
  40.    
  41.     public static byte[] getBytes(long x)
  42.     {
  43.         return new byte[] {
  44.             (byte)(x >>> 56),
  45.             (byte)(x >>> 48),
  46.             (byte)(x >>> 40),
  47.             (byte)(x >>> 32),
  48.             (byte)(x >>> 24),
  49.             (byte)(x >>> 16),
  50.             (byte)(x >>> 8),
  51.             (byte)x
  52.                 };
  53.     }
  54.    
  55.     public static byte[] getBytes(float x)
  56.     {
  57.         return getBytes(
  58.             Float.floatToRawIntBits(x));
  59.     }
  60.    
  61.     public static byte[] getBytes(String x)
  62.     {
  63.         return x.getBytes();
  64.     }
  65.    
  66.     public static long doubleToInt64Bits(double x)
  67.     {
  68.         return Double.doubleToRawLongBits(x);
  69.     }
  70.    
  71.     public static double int64BitsToDouble(long x)
  72.     {
  73.         return (double)x;
  74.     }
  75.    
  76.     public boolean toBoolean(byte[] bytes, int index)
  77.     {
  78.         return bytes[index] != 0;
  79.     }
  80.    
  81.     public char toChar(byte[] bytes, int index)
  82.     {
  83.         return (char)bytes[index];
  84.     }
  85.    
  86.     public double toDouble(byte[] bytes, int index)
  87.     {
  88.         return Double.longBitsToDouble(
  89.             toInt64(bytes, index));
  90.     }
  91.    
  92.     public static short toInt16(byte[] bytes, int index)
  93.     {
  94.         return (short)(
  95.             (0xff & bytes[index]) << 8   |
  96.             (0xff & bytes[index + 1]) << 0
  97.             );
  98.     }
  99.    
  100.     public static int toInt32(byte[] bytes, int index)
  101.     {
  102.         return (int)(
  103.             (int)(0xff & bytes[index]) << 56  |
  104.             (int)(0xff & bytes[index + 1]) << 48  |
  105.             (int)(0xff & bytes[index + 2]) << 40  |
  106.             (int)(0xff & bytes[index + 3]) << 32
  107.             );
  108.     }
  109.    
  110.     public static long toInt64(byte[] bytes, int index)
  111.     {
  112.         return (long)(
  113.             (long)(0xff & bytes[index]) << 56  |
  114.             (long)(0xff & bytes[index + 1]) << 48  |
  115.             (long)(0xff & bytes[index + 2]) << 40  |
  116.             (long)(0xff & bytes[index + 3]) << 32  |
  117.             (long)(0xff & bytes[index + 4]) << 24  |
  118.             (long)(0xff & bytes[index + 5]) << 16  |
  119.             (long)(0xff & bytes[index + 6]) << 8   |
  120.             (long)(0xff & bytes[index + 7]) << 0
  121.             );
  122.     }
  123.    
  124.     public static float toSingle(byte[] bytes, int index)
  125.     {
  126.         return Float.intBitsToFloat(
  127.             toInt32(bytes, index));
  128.     }
  129.    
  130.     public static String toString(byte[] bytes)
  131.     {
  132.         return new String(bytes);
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement