>It outputs 0 when I would expect 2.
The problem is with your expectation. You're punning the bytes of a multi-byte object(float) to raw binary[1], then casting the first byte[2] of the result to another multi-byte type (int) and somehow expecting the value to match what was originally stored in the original multi-byte object. This is ignoring the non-trivial byte representation of floating-point that doesn't map well to integers in a direct conversion.