Java存储二进制数字文件,高位优先与低位优先

import java.io.IOException;
import java.io.RandomAccessFile;


public class TransHEXUtil {

    public static void write(int a, RandomAccessFile raf) throws IOException {
        int low1 = a & 0x000000ff;
        int low2 = a & 0x0000ff00;
        int high1 = a & 0x00ff0000;
        int high2 = a & 0xff000000;
        low1 = low1 << 24;
        low2 = low2 << 8;
        high1 = high1 >> 8;
        high2 = high2 >> 24;
        int b = low2 | low1 | high2 | high1;
        raf.writeInt(b);
    }

    public static int read(RandomAccessFile raf) throws IOException {
        int low1 = raf.read();
        int low2 = raf.read();
        int high1 = raf.read();
        int high2 = raf.read();
        low1 = low1 << 0;
        low2 = low2 << 8;
        high1 = high1 << 16;
        high2 = high2 << 24;
        int b = low2 | low1 | high2 | high1;
        return b;
    }
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×