Java little endian binary output

Recent and C language interactive system, you need to transfer binary files to each other, but the C language files by default is small end, but as a Java web application, the default is the big end

Therefore we need to modify the order of bytes written at the time of writing binary files.

Take a look at the normal write

FileOutputStream fos = new FileOutputStream("D:/temp/test.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(bos);

dos.writeShort(66);

dos.close();
bos.close();
fos.close();

Hexadecimal editor to open the file contents can be seen as

00 42

However, the order is the C language small end first, i.e.,

42 00

View DataOutputStream.writeShort source, in fact, it will also be broken down into 8-bit binary value (1byte) write

public final void writeShort(int v) throws IOException {
    out.write((v >>> 8) & 0xFF);
    out.write((v >>> 0) & 0xFF);
    incCount(2);
}

So we can write a byte array manually, the manual target value into a byte array, so we write a method

// 小端输出模式
public static byte[] short2Bytes(short a) {
    return new byte[] {
            (byte) (a & 0xFF),
            (byte) ((a >> 8) & 0xFF)
    };
}

Modify call, the beginning of

dos.writeShort(66);

Changed

dos.write(short2Bytes((short)66));

The output file is small end of the first.

The above example is a write short. int is the same reason, to write a manual method to transfer byte int array

// 小端输出模式
public static byte[] int2Bytes(int a) {
    return new byte[] {
            (byte) (a & 0xFF),
            (byte) ((a >> 8) & 0xFF),
            (byte) ((a >> 16) & 0xFF),
            (byte) ((a >> 24) & 0xFF)
    };
}

When the call

dos.writeInt(66);

Changed

dos.write(int2Bytes(66));

As long, in accordance with the law, I believe that readers can write their own.

Finally, attach the complete demo code

package smallend;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;

public class SmallEndTest {

    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("D:/temp/test.txt");
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        DataOutputStream dos=new DataOutputStream(bos);
        
        dos.writeShort(66); // 00 42
        dos.write(short2Bytes((short)66)); // 42 00
        
        dos.writeInt(66); // 00 00 00 42
        dos.write(int2Bytes(66)); // 42 00 00 00
        
        dos.close();
        bos.close();
        fos.close();

    }
    
    // 小端输出模式
    public static byte[] short2Bytes(short a) {
        return new byte[] {
                (byte) (a & 0xFF),
                (byte) ((a >> 8) & 0xFF)
        };
    }
    
    // 小端输出模式
    public static byte[] int2Bytes(int a) {
        return new byte[] {
                (byte) (a & 0xFF),
                (byte) ((a >> 8) & 0xFF),
                (byte) ((a >> 16) & 0xFF),
                (byte) ((a >> 24) & 0xFF)
        };
    }

}

Tucao about: the Internet looking for a long blog, content method a lot, do not know in the end is shocked by how bad [about] my comprehension. Finally get out on his own, and hereby record it.

Guess you like

Origin www.cnblogs.com/tenny-peng/p/11847364.html