Convert Hex code to exe file using Java

Dev01

I was trying to create a sort of compressing/decompressing algorithm and now that works I was thinking on how to write down to file its decompressed hex code. I tried to convert it in an array of byte but something strange happens:
the result file is bigger than the original one even if the decompressed hex string and the original one are equal. When I try to open it, it gives me Compatibility issue with current Windows version
To create the decompressed exe and so writing in it its hex code I used this code:
FileOutputStream stream=new FileOutputStream("Hello2.exe"); stream.write(hex.getBytes()); stream.close(); Any suggestions?

Sizik

The problem is that you're reading the bytes into a hex string, then writing the hex string directly to the file without converting it back to the appropriate bytes first.

Let's say the file contents are hello world. As an array of bytes, this is {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64}. After you read it into a string, you have "48656c6c6f20776F726C64". When you call getBytes() on this string, you get the following byte array back: {0x34, 0x38, 0x36, 0x35, 0x36, 0x63, 0x36, 0x63, 0x36, 0x66, 0x32, 0x30, 0x37, 0x37, 0x36, 0x46, 0x37, 0x32, 0x36, 0x43, 0x36, 0x34}. Thus, your output file is 48656c6c6f20776F726C64, rather than hello world.

This is because you're getting the byte representation of each individual character in the string, NOT the byte values of the hex pairs represented by the string.

See this question for a way to convert the string to bytes correctly: Convert a string representation of a hex dump to a byte array using Java?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert hex code in string to hex code in byte (java)

From Dev

How to convert java or class file to exe file

From Dev

Run .exe file of C code using Eclipse

From Dev

How to convert hex to decimal rgb565 using java?

From Dev

Hex code using Python

From Dev

Convert RAL to HEX color code

From Dev

Strange color code? Convert to Hex?

From Dev

Convert intel hex string to normal hex in java

From Dev

Convert Hex to Binary on large file

From Dev

Convert hex file to floats in php

From Dev

Convert pdb to exe file?

From Dev

Remove a character from java string using hex code

From Dev

I'm not able to convert my python file into a exe using pyinstaller

From Dev

Convert Hex to String using VbScript

From Dev

Convert Hex to String using VbScript

From Dev

Using regex to convert hex to ascii

From Java

How can I convert my Java program to an .exe file?

From Dev

Convert a large number to hex in Java

From Dev

Convert an XML file to CSV file using java

From Dev

Convert .txt file into .cvs file using java?

From Dev

Building an executable file for python code using py2exe

From Dev

Find a constraint using the hex code

From Dev

Convert hex color code to color name

From Dev

Convert Hex code to Date (BLE) - Algorithm

From Dev

Generating hex file using gcc

From Dev

Python: Is original source code viewable when converting a python script to .exe file using py2exe?

From Dev

convert hex(png) file to rgb values

From Dev

Best way to convert Hex string file to integer

From Dev

convert hex values to decimal in a text file