/** * Some experiments to help me think about splitting integers and * other types into bytes. * * @author Samuel A. Rebelsky * @version 0.1 of December 2002 */ public class Bytes { /** * Print out all of memory in a somewhat easy-to-read form. */ public static void dumpMemory(byte[] memory) { for (int i = 0; i < memory.length; i++) { if ((i % 8) == 0) { if (i != 0) System.out.println(); if (i < 10) System.out.print("000"); else if (i < 100) System.out.print("00"); else if (i < 1000) System.out.print("0"); System.out.print(i + ": "); } String b = Integer.toHexString(((int) memory[i]) & 0x000000FF); if (b.length() == 1) System.out.print(" 0" + b); else System.out.print(" " + b); } // for(i) System.out.println(); } // dumpMemory(byte[]) /** * Retrieve a float (4 bytes) starting at a particular location in memory. * * @exception Exception * If the memory location is not 4-aligned. */ public static float retrieveFloat(byte[] memory, int pos) throws Exception { return Float.intBitsToFloat(retrieveInt(memory, pos)); } // retrieveFloat(byte[], int) /** * Retrieve an integer (4 bytes) starting at a particular location in memory. * * @exception Exception * If the memory location is not 4-aligned. */ public static int retrieveInt(byte[] memory, int pos) throws Exception { if ((pos % 4) != 0) throw new Exception("Integer accesses must be 4-aligned."); int i; i = memory[pos]; i = (i << 8) | (memory[pos+1] & 0x000000FF); i = (i << 8) | (memory[pos+2] & 0x000000FF); i = (i << 8) | (memory[pos+3] & 0x000000FF); return i; } // retrieveInt(byte[], int) /** * Retrieve a long (8 bytes) starting at a particular location. * * @exception Exception * If the memory location is no 8-aligned. */ public static long retrieveLong(byte[] memory, int pos) throws Exception { if ((pos % 8) != 0) throw new Exception("Long accesses must be 8-aligned."); return (((long) retrieveInt(memory, pos)) << 32) | (((long) retrieveInt(memory,pos+4)) & 0x00000000FFFFFFFF) ; } // retrieveLong(byte[], int) /** * Store a float (4 bytes) starting at a particular location in memory. * * @exception Exception * If the memory location is not 4-aligned. */ public static void storeFloat(byte[] memory, int pos, float val) throws Exception { storeInt(memory, pos, Float.floatToIntBits(val)); } // storeFloat(byte[], int, float) /** * Store an integer (4 bytes) starting at a particular location in memory. * * @exception Exception * If the memory location is not 4-aligned. */ public static void storeInt(byte[] memory, int pos, int val) throws Exception { if ((pos % 4) != 0) throw new Exception("Integer accesses must be 4-aligned."); memory[pos] = (byte) (val >>> 24); memory[pos+1] = (byte) (val >>> 16); memory[pos+2] = (byte) (val >>> 8); memory[pos+3] = (byte) val; } // storeInt(byte[], int, int) /** * Store a long (8 bytes) starting at a particular location in memory. * * @exception Exception * If the memory location is not 8-aligned. */ public static void storeLong(byte[] memory, int pos, long val) throws Exception { if ((pos % 8) != 0) throw new Exception("Long accesses must be 8-aligned."); storeInt(memory, pos, (int) (val >>> 32)); storeInt(memory, pos+4, (int) val); } // storeLong(byte[], int, int) // +---------------+----------------------------------------------------- // | Various Tests | // +---------------+ /** * Print the sequence base, base*2, base*4, .... */ public static void printPowerSequence(int base) { int i; int pow; for (pow = 0; pow < 33; pow++) { i = base << pow; System.out.println("i: " + Integer.toBinaryString(i) + " (" + i + ")"); } } // printPowerSequence(int) /** * Convert an integer to a byte and print out all the * results. */ public static void testIntToByte(int i) { byte b; System.out.println("i: " + Integer.toBinaryString(i) + " (" + i + ")"); b = (byte) (i & 0x000000FF); System.out.println("b: " + Integer.toBinaryString(b) + " (" + b + ")" ); int bprime = (((int) b) << 24) >>> 24; System.out.println("b': " + Integer.toBinaryString(bprime) + " (" + bprime + ")" ); } // testIntToByte(int) // +------+-------------------------------------------------------------- // | Main | // +------+ public static void main(String[] args) throws Exception { byte[] memory = new byte[32]; for (int i = 0; i < args.length; i++) storeLong(memory, i*8, Long.parseLong(args[i])); dumpMemory(memory); for (int i = 0; i < args.length; i++) System.out.println(retrieveLong(memory, i*8)); } // main(String[]) } // class Bytes