Binary file to hexadecimal string

The first version

public static String binFileToHexStrV1(File file) {

        StringBuilder hexStr = new StringBuilder();

        try (
                DataInputStream bin = new DataInputStream(new FileInputStream(file));
        ) {
            byte temp = 0;
            for (int i = 0; i < file.length(); i++) {
                temp = bin.readByte();
                // Returns a string representation as an unsigned hexadecimal integer.
                String str = Integer.toHexString(temp);
                if (str.length() == 8) {
                    // remove the complement f
                    str = str.substring(6);
                }
                if (str.length() == 1) {
                    str = "0"+ str; } hexStr.append(str.toUpperCase()); }}catch (Exception e) {
            e.printStackTrace();
        }

        return hexStr.toString();

    }
Copy the code

It takes 5397 milliseconds to read a 100K binary

The second version

 public static String binFileToHexStrV2(File file) {

        StringBuilder hexStr = new StringBuilder();

        try (
                DataInputStream bin = new DataInputStream(new FileInputStream(file));
        ) {
            for (int i = 0; i < file.length(); i++) {
                hexStr.append(String.format("%02X", bin.read())); }}catch (Exception e) {
            e.printStackTrace();
        }

        return hexStr.toString();

    }

Copy the code

It takes 6832 milliseconds to read a 100K binary

The third version

public static String binFileToHexStrV3(File file) {

        StringBuilder hexStr = new StringBuilder();

        byte[] data = new byte[1024*5];

        try (
                DataInputStream bin = new DataInputStream(new FileInputStream(file));
        ) {
            while (true) {

                int a = 0;

                a = bin.read(data);

                if (a == -1) {
                    break;
                }
                byte[] data2 = new byte[a];

                for (int i = 0; i < a; i++) {
                    data2[i] = data[i];
                }
                hexStr.append(DatatypeConverter.printHexBinary(data2));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return hexStr.toString();

    }
Copy the code

It takes 21 milliseconds to read a 100K binary

The fourth edition

public static String binFileToHexStrV4(File file) {

        char[] hexCode = "0123456789ABCDEF".toCharArray();

        StringBuilder hexStr = new StringBuilder();

        byte[] data = new byte[1024*5];

        try (
                DataInputStream bin = new DataInputStream(new FileInputStream(file));
        ) {
            while (true) {

                int a = 0;

                a = bin.read(data);

                if (a == -1) {
                    break;
                }

                for (int i = 0; i < a; i++) {
                    hexStr.append(hexCode[(data[i] >> 4) & 0xF]);
                    hexStr.append(hexCode[(data[i] & 0xF)]); }}}catch (Exception e) {
            e.printStackTrace();
        }

        return hexStr.toString();

    }

Copy the code

It takes 7 milliseconds to read a 100K binary

Finally, I took off my slow hat.

Summary: When reading files, we should adjust the number of reads and cycles appropriately to reduce unnecessary time consuming.