Hi All,
Im desperate to write a hex file in Delphi.
Effectively I have a Decimal value, say 580 which I convert to Hex: 02 44

And I need to put this into a Hex file (multiple times) - 25000 times per file

Each line in a similar hex file has
00000000h: 02 44 02 45 02 4D 02 44 02 45 02 4D 02 44 02 45 02 4D 02 4D
00000010h: 02 44 02 45 02 4D 02 44 02 45 02 4D 02 44 02 45 02 4D 02 4D

And my file needs to follow this format.
I assume this means there are 4 digit hex values per address??

But then again I have assumed many things that are wrong.

Help me OB1-Kanobis your my only help.

What you mean with "hex file"?

If it's a binary file, you can use TFileStream to write your bytes into a file.

A few days ago I had this exact problem. This is the solution I implemented.

You need a HexEditor that can SAVA AS ASCII. for example HexProbe

You also need a text editor to REPLACE a string by another in the resulting ASCII file.

// Generated by Hexprobe Version 4.2
// http://www.hexprobe.com/
//
// Original File: SEARCH.GIF
// Data Offset: 0000000000 / $0000000000
// Data Length: 0000004101 / $0000001005 (bytes)
// LENGTH OF ARRAY 4101

Use Write instead of WriteLn (avoid CR+LF at the end)

if FileExists(App_Path+'Search.gif') then
begin
*
* No problems
*
end else
begin // create it first
gif_exists := false;
s:='';
for i:= 1 to 4101 do s:=s+chr(MyGIFText);
assignfile(f,App_Path+'Search.gif');
rewrite(f);
write(f,s);
closefile(f);

*
* No problems...
*
end;
end;

{Result of HeProbe SAVE AS and the text editor's Replace: }

MyDBFText : Array[1..4101] of byte = (

$47, $49, $46, $38, $39, $61, $63, $00, $34, $00, $87, $00, $00, $00, $00, $00,
$00, $00, $33, $00, $00, $66, $00, $00, $99, $00, $00, $CC, $00, $00, $FF, $33,
$00, $00, $33, $00, $33, $33, $00, $66, $33, $00, $99, $33, $00, $CC, $33, $00,
*
*
};

was originally

0X47, 0X49, 0X46, 0X38, 0X39, 0X61, 0X63, 0X00, 0X34, 0X00, 0X87, 0X00, 0X00, 0X00, 0X00, 0X00,
0X00, 0X00, 0X33, 0X00, 0X00, 0X66, 0X00, 0X00, 0X99, 0X00, 0X00, 0XCC, 0X00, 0X00, 0XFF, 0X33,
0X00, 0X00, 0X33, 0X00, 0X33, 0X33, 0X00, 0X66, 0X33, 0X00, 0X99, 0X33, 0X00, 0XCC, 0X33, 0X00,
*
*
Changed all '0X' into '$' that's all...

Eitan Gilboa
June 17th 2008

Thankyou for your response.
Ive actually created the data myself, as I need to query many files to acquire the data then write the hex file.
I am hoping I am correct with the data I retrieve first then I need to put it into hex format.
So judging by your response I can go straight to writing the file in hex format?

yes / no?

Im a little lost.

sorry

The "hex" file is really just a plain-vanilla text file.

Each byte is represented by a two-digit hexadecimal value: 00..FF.

The first number is the relative address of the first byte listed on that line. In the example you listed, the first byte listed on the first line is at offset 0 --> 00000000h: . There are 16 bytes listed per line, so the next line is at offset 16 --> 00000010h , and the line after that is at offset 32 --> 00000020h , etc.

So, all you have to do is open your input file as a file of byte, then read it 16 bytes at a time (16 hexadecimal codes per line of output, remember). If you are using Delphi, you will find the IntToHex function (in the SysUtils unit) very helpful.

Good luck!

This article has been dead for over six months. Start a new discussion instead.