Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.hex file generation

Status
Not open for further replies.

RichieMac

Programmer
Joined
Aug 18, 2004
Messages
61
Location
GB
Hi guys,

A little query is all. Could anyone tell me if it is at all possible to take a ordinary text file and convert it into a .hex file for PROM blowing using Java. If so a nudge in the right direction would be greatly appreciated.

Thanks all in advance.
 
Read the file into a byte[] array, and try the below, which was taken from here :

Code:
    public static String encode(/*const*/ byte[] data, int off, int len)
    {
        char[]	ch;
        int	i;

        // Convert bytes to hex digits
        ch = new char[data.length*2];
        i = 0;

        while (len-- > 0)
        {
            int		b;
            int		d;

            // Convert next byte into a hex digit pair
            b = data[off++] & 0xFF;

            d = b >> 4;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;

            d = b & 0xF;
            d = (d < 0xA ? d+'0' : d-0xA+'A');
            ch[i++] = (char) d;
        }

        return (new String(ch));
    }

--------------------------------------------------
Free Database Connection Pooling Software
 
So once the data is converted to hex format do I then just write the .hex file in the normal way.

i.e. File outputFile = new File("out.hex");
FileWriter out = new FileWriter(outputFile);
out.write(SOME DATA);

or something along those lines.
 
Something like this :

Code:
File f = ...
FileInputStream fis ...
byte[] data = new byte[(int)f.length()];
fis.read(data);
fis.close();

String hex = encode(data);

FileOutputStream fos ...
fos.write(hex);
fos.close();

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top