Java tutorial
//package com.java2s; public class Main { public static byte[] toByta(byte data) { return new byte[] { data }; } public static byte[] toByta(byte[] data) { return data; } public static byte[] toByta(short data) { return new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } public static byte[] toByta(short[] data) { if (data == null) return null; // ---------- byte[] byts = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) System.arraycopy(toByta(data[i]), 0, byts, i * 2, 2); return byts; } public static byte[] toByta(char data) { return new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } public static byte[] toByta(char[] data) { if (data == null) return null; // ---------- byte[] byts = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) System.arraycopy(toByta(data[i]), 0, byts, i * 2, 2); return byts; } public static byte[] toByta(int data) { return new byte[] { (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } public static byte[] toByta(int[] data) { if (data == null) return null; // ---------- byte[] byts = new byte[data.length * 4]; for (int i = 0; i < data.length; i++) System.arraycopy(toByta(data[i]), 0, byts, i * 4, 4); return byts; } public static byte[] toByta(long data) { return new byte[] { (byte) ((data >> 56) & 0xff), (byte) ((data >> 48) & 0xff), (byte) ((data >> 40) & 0xff), (byte) ((data >> 32) & 0xff), (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } public static byte[] toByta(long[] data) { if (data == null) return null; // ---------- byte[] byts = new byte[data.length * 8]; for (int i = 0; i < data.length; i++) System.arraycopy(toByta(data[i]), 0, byts, i * 8, 8); return byts; } public static byte[] toByta(float data) { return toByta(Float.floatToRawIntBits(data)); } public static byte[] toByta(float[] data) { if (data == null) return null; // ---------- byte[] byts = new byte[data.length * 4]; for (int i = 0; i < data.length; i++) System.arraycopy(toByta(data[i]), 0, byts, i * 4, 4); return byts; } public static byte[] toByta(double data) { return toByta(Double.doubleToRawLongBits(data)); } public static byte[] toByta(double[] data) { if (data == null) return null; // ---------- byte[] byts = new byte[data.length * 8]; for (int i = 0; i < data.length; i++) System.arraycopy(toByta(data[i]), 0, byts, i * 8, 8); return byts; } public static byte[] toByta(boolean data) { return new byte[] { (byte) (data ? 0x01 : 0x00) }; // bool -> {1 byte} } public static byte[] toByta(boolean[] data) { // Advanced Technique: The byte array containts information // about how many boolean values are involved, so the exact // array is returned when later decoded. // ---------- if (data == null) return null; // ---------- int len = data.length; byte[] lena = toByta(len); // int conversion; length array = lena byte[] byts = new byte[lena.length + (len / 8) + (len % 8 != 0 ? 1 : 0)]; // (Above) length-array-length + sets-of-8-booleans +? byte-for-remainder System.arraycopy(lena, 0, byts, 0, lena.length); // ---------- // (Below) algorithm by Matthew Cudmore: boolean[] -> bits -> byte[] for (int i = 0, j = lena.length, k = 7; i < data.length; i++) { byts[j] |= (data[i] ? 1 : 0) << k--; if (k < 0) { j++; k = 7; } } // ---------- return byts; } public static byte[] toByta(String data) { return (data == null) ? null : data.getBytes(); } public static byte[] toByta(String[] data) { // Advanced Technique: Generates an indexed byte array // which contains the array of Strings. The byte array // contains information about the number of Strings and // the length of each String. // ---------- if (data == null) return null; // ---------- flags: int totalLength = 0; // Measure length of final byte array int bytesPos = 0; // Used later // ----- arrays: byte[] dLen = toByta(data.length); // byte array of data length totalLength += dLen.length; int[] sLens = new int[data.length]; // String lengths = sLens totalLength += (sLens.length * 4); byte[][] strs = new byte[data.length][]; // array of String bytes // ----- pack strs: for (int i = 0; i < data.length; i++) { if (data[i] != null) { strs[i] = toByta(data[i]); sLens[i] = strs[i].length; totalLength += strs[i].length; } else { sLens[i] = 0; strs[i] = new byte[0]; // prevent null entries } } // ---------- byte[] bytes = new byte[totalLength]; // final array System.arraycopy(dLen, 0, bytes, 0, 4); byte[] bsLens = toByta(sLens); // byte version of String sLens System.arraycopy(bsLens, 0, bytes, 4, bsLens.length); // ----- bytesPos += 4 + bsLens.length; // mark position // ----- for (byte[] sba : strs) { System.arraycopy(sba, 0, bytes, bytesPos, sba.length); bytesPos += sba.length; } // ---------- return bytes; } }