/** * This class provides conversion routines to convert between positive integers * and arrays of bytes representing the integer in binary. The public methods here * are: * * padArray pads a byte array with leading zeros * int2ByteArray converts an int to binary array of bytes of 0's and 1's * byteArray2Int converts an array of bytes of 0's and 1's to an int * int2Bits converts an integer to a String of 0's and 1's * bits2Int converts a String of 0's and 1's to an integer * bytes2String converts an array of bytes into a String representation * * Examples: * * int x = 131; * byte[] bytes = Convert.int2ByteArray(x); // gives [1 0 0 0 0 0 1 1] * System.out.println(Convert.byteArry2String(bytes)); // shows { 1, 0, 0, 0, 0, 0, 1, 1 } * byte[] bigBytes = Convert.padArray(bytes, 10); // gives [0 0 1 0 0 0 0 0 1 1] * int y = Convert.byteArray2Int(bigBytes); // gives back 131 * * @author Bert G. Wachsmuth * @version 1.0 (Oct. 2010) */ public class Convert { public static void main(String args[]) { byte[] a = {1, 0, 1, 0}; int x = byteArray2Int(a); System.out.println(x); } /* * Pads a byte array with leading zeros if necessary. * * Assumption: the input array has positive length <= size. * * Example: * * bytes[] a = {1, 0, 1, 0}; * bytes[] b = padArray(a, 10) * * returns {0, 0, 0, 0, 0, 0, 1, 0, 1, 0} */ public static byte[] padArray(byte[] a, int size) { if (a.length > size) throw new IndexOutOfBoundsException("length of input array larger than padded array"); if (a.length == size) return a; byte[] output = new byte[size]; for (int i = 0; i < a.length; i++) { output[size - i - 1] = a[a.length - i - 1]; } return output; } /* * Converts a positive integer to an array of bytes, representing the integer * as a binary number. * * Assumption: the input integer is positive * * Example: * * byte[] bytes = int2ByteArray(131); * * gives [1 0 0 0 0 0 1 1] */ public static byte[] int2ByteArray(int x) { String bits = int2Bits(x); byte[] output = new byte[bits.length()]; for (int i = 0; i < bits.length(); i++) { if (bits.charAt(i) == '1') output[i] = 1; else output[i] = 0; } return output; } /* * Converts an array representing a binary number into the equivalent integer. * * Assumption: input array has length > 0 * * Example: * * byte[] a = {1, 0, 1, 0}; * int x = byteArray2Int(a); * * returns x = 10. */ public static int byteArray2Int(byte[] number) { String bits = ""; for (int i = 0; i < number.length; i++) { if (number[i] == 1) bits += "1"; else bits += "0"; } return bits2Int(bits); } /* * Converts a byte array to a String list of bits in binary format. * * Assumption: input array has length > 0 * * Example: * byte[] a = {1, 0, 1, 0}; * String s = byteArray2String(a); * * returns "{ 1, 0, 1, 0 }" */ public static String byteArry2String(byte[] bytes) { String list = "{ "; for (int i = 0; i < bytes.length-1; i++) list += bytes[i] + ", "; list += bytes[bytes.length-1] + " }"; return list; } /* * Converts a positive int to a String of 0's and 1's, equal to the binary * representation of that number. * * Assumption: input integer is positive * * Example: * * String s = int2Bits(131); * * returns "10000011" */ public static String int2Bits(int x) { if (x == 0) return "0"; else if (x == 1) return "1"; else return int2Bits(x / 2) + (x % 2); } /* * Converts a String representing a binary numbers into the * equivalent int. * * Assumption: input String is not empty. * * Example: * * int x = bits2Int("10000011"); * * returns 131 */ public static int bits2Int(String bits) { int num = 0; for (int i = 0; i < bits.length(); i++) { char cc = bits.charAt(bits.length()-i-1); if (cc == '1') num = num + (int)Math.pow(2, i); } return num; } }