user2638179
user2638179

Reputation: 23

Hexa in string to file in java

I have String that contains hexa data. I wish to save it as raw hexa file.

So i have string like :

String str ="0105000027476175675C6261636B6772";

What i have to do , to get file.hex that will have same data in byte.

When i was trying that :

PrintStream out = new PrintStream("c:/file.hex");
out.print(str);

i get file that have

"0105000027476175675C6261636B6772"

but in hexa it is:

30 31 30 35 30 30 30 30 32 37 34 37 36 31 37 35 36 37 35 43 36 32 36 31 36 33 36 42 36 37 37 32

My goal is to have file that in hexa it will have

01 05 00 00 27 47 61 75 67 5C 62 61 63 6B 67 72

Upvotes: 2

Views: 154

Answers (3)

MightyPork
MightyPork

Reputation: 18861

Two hexa digits make a byte.

File file = new File("yourFilePath");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

Now, loop through the string, taking two chars at a time using string.substring() method.

You would think Byte.parseByte(theTwoChars, 16) is a good option here, but it will fail miserably, because it thinks the byte is signed. You should instead use:

byte b = (byte) ( Integer.parseInt(theTwoChars, 16) & 0xFF )

You can either write the bytes one-by-one, or construct an array to store them in.

To write a byte or byte[] to your output stream:

bos.write(bytes);

Finally, close the stream:

bos.close();

Here's a fully working method I made to demonstrate it:

public static void bytesToFile(String str, File file) throws IOException {
    BufferedOutputStream bos = null;
    try {

        // check for invalid string    
        if(str.length() % 2 != 0) {
            throw new IllegalArgumentException("Hexa string length is not even.");
        }

        if(!str.matches("[0-9a-fA-F]+")) {
            throw new IllegalArgumentException("Hexa string contains invalid characters.");
        }

        // prepare output stream
        bos = new BufferedOutputStream(new FileOutputStream(file));

        // go through the string and make a byte array
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            String twoChars = str.substring(2 * i, 2 * i + 2);

            int asInt = Integer.parseInt(twoChars, 16);

            bytes[i] = (byte) (asInt & 0xFF);
        }

        // write bytes
        bos.write(bytes);

    } finally {
        if (bos != null) bos.close();
    }
}

Upvotes: 4

Joop Eggen
Joop Eggen

Reputation: 109567

assert str.length() % 2 == 0; // Two hexadecimal digits for a byte.
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < bytes.length; ++i) {
    String byteRepr = str.substring(2 * i, 2 * i + 2);
    byte b = Byte.parseByte(byteRepr, 16); // Base 16
    bytes[i] = b;
}

Files.write(Paths.get("c:/file.bin"), bytes, StandardOpenOption.WRITE);
// Or use a FileOutputStream with write(bytes);

(About terms: hexadecimal, coming from Greek, means 16, the counting base; one digit 0-9-A-F being able to represent 4 bits.)

Upvotes: 2

oddparity
oddparity

Reputation: 436

You will have to convert your string to a short array and store this.

Upvotes: 0

Related Questions