Just use NIO. It is intended for this specific purpose. ByteBuffer and IntBuffer will do what you need quickly, efficiently and elegantly. It will handle large / small final conversions, "direct" buffers for high-performance I / O, and you can even mix data types into a byte buffer.
Converting integers to bytes:
ByteBuffer bbuffer = ByteBuffer.allocate(4*theIntArray.length); IntBuffer ibuffer = bbuffer.asIntBuffer();
Convert bytes to integers:
ByteBuffer bbuffer = ByteBuffer.wrap(rawBytes); IntBuffer ibuffer = bbuffer.asIntBuffer(); while(ibuffer.hasRemaining()) System.out.println(ibuffer.get());
James Schek Nov 26 '08 at 21:15 2008-11-26 21:15
source share