sbutler.com

Binary data loading in Java

I need to get something off my chest: DataInputStream.readFloat(), I hate you!! Today I had one of those annoying bugs pop up that are hard to chase down. I know how to read/write my binary IEEE754 floats in C (all tested and working) but my Java code is really not liking it. Turns out readFloat() doesn’t actually know how to read a float at all. hmm. Oh well, here is the code to load yourself:

bits = 0;
for (int i = 0; i < 32; i += 8 )
bits |= (int) dis.read() << i;
data[w][h] = Float.intBitsToFloat(bits);

Comments are closed.