Re: Another topic on how to read a binary file.
Mark Space wrote:
Does the static method Integer.reverseBytes(int) do the same thing? It
seems like it should, although I didn't check it. DataInputStream seems
like the better choice for this case, just wanted to point out there was
another method...
Answer: yes it does, although my quick test seems to have been bit by a
silent cast conversion. To the language designers: dear sweet Jesus and
God in Heaven, why?
package integertest;
import java.lang.Integer;
public class Main
{
public static void main(String[] args)
{
System.out.println( "128 reversed is " +
Integer.reverseBytes(128));
System.out.println( "-1 reversed is " + Integer.reverseBytes(-1));
System.out.println( "-129 reversed is " +
Integer.reverseBytes(-129));
System.out.println( "256 reversed is " +
Integer.reverseBytes(256));
}
}
ompile:
run:
128 reversed is -2147483648
-1 reversed is -1
-129 reversed is 2147483647
256 reversed is 65536
BUILD SUCCESSFUL (total time: 0 seconds)