Board index » delphi » Intel Hex file conversion

Intel Hex file conversion

Since I had no response to a previous posting I will try once more.

Has anybody done this before or knows of a suitable pascal source to
convert an Intel Hex file to binary format.

The strings in TMemo.Lines look like such:

FD1300008002800602808175A80075818075D000C24B

The above string needs to be converted so that the result is equivalent
binary presentation. So two bytes are to be converted into one, the
resulting binary string being half the length of the original.

I have been unsuccessful in finding any string conversion utility which
can do this.

Any help is appreciated.

Helmut

 

Re:Intel Hex file conversion


Quote
On Sat, 29 Mar 1997, Helmut Strickner wrote:
> The strings in TMemo.Lines look like such:

> FD1300008002800602808175A80075818075D000C24B

> The above string needs to be converted so that the result is equivalent
> binary presentation. So two bytes are to be converted into one, the
> resulting binary string being half the length of the original.

> I have been unsuccessful in finding any string conversion utility which
> can do this.

        Hey, why do you need a component? It's really simple. Something
like:

let's assume s is your string. It's lendth is always even, and it's all
upper case. And pb is a ^byte pointer to the binary buffer.

var c1,c2:char;b1,b2:byte;

for i:=0 to length(s) div 2-1 do
begin
 c1:=s[i*2+1];
 c2:=s[i*2+2];

if c1 in ['0'..'9'] then b1:=(ord(c1)-ord('0'))
else if c1 in ['A'..'F']) then b1:=(ord(c1)-ord('A'))
else raise Exception.Create('Invalid character in line:'+c1);

if c2 in ['0'..'9'] then b2:=(ord(c2)-ord('0'))
else if c2 in ['A'..'F']) then b2:=(ord(c2)-ord('A'))
else raise Exception.Create('Invalid character in line:'+c2);

pb^:=b1*16+b2; inc(pb);

end;

Unchecked - I just typed it.

Alex.

Re:Intel Hex file conversion


Quote
Helmut Strickner <apo...@gil.com.au> wrote:
>Since I had no response to a previous posting I will try once more.
>Has anybody done this before or knows of a suitable pascal source to
>convert an Intel Hex file to binary format.
>The strings in TMemo.Lines look like such:
>FD1300008002800602808175A80075818075D000C24B
>The above string needs to be converted so that the result is equivalent
>binary presentation. So two bytes are to be converted into one, the
>resulting binary string being half the length of the original.
>I have been unsuccessful in finding any string conversion utility which
>can do this.

Hi Helmut

I can maybe help, but could you send a litle more of the file, because
it seems that it is the 'small' Intel-Hex file you have, and the reading
and writing I have done was on the 'big' version ( can handle over 64K ).

And shortly I can tell that every line has a record-type, length, data
and a checksum field.
---------------------------------------------
  Ove Kjeldgaard
    !o_kj...@post4.tele.dk
      Remove the ! if you wish to e-mail me
---------------------------------------------

Re:Intel Hex file conversion


Thanks to everyone who replied to my posting.

In the meantime I have come up with a solution to my question.

Here a snippet:

   for c:= 1 to (Length(S)-1) do
   {first string}
   begin
     if S[c] in ['A'..'F'] then
     begin
       i1 := Ord(S[c])-$37;
       i2 := i1 shl 4;       {shift low nibble to high nibble}
     end
     else
       if S[c] in ['0'..'9'] then
       begin
         i1 := Ord(S[c])-$30;
         i2 := i1 shl 4;     {shift low nibble to high nibble}
       end;
      {second string}
       if S[c+1] in ['A'..'F'] then
       begin
         i1 := Ord(S[c+1])-$37;
         i2 := i2 xor i1;    {xor first and second , result now in i2}
       end
       else
         if S[c+1] in ['0'..'9'] then
         begin
           i1 := Ord(S[c+1])-$30;
           i2 := i2 xor i1;  {xor first and second , result now in i2}
         end;
         if c < (Length(S)-1) then inc (c);  {increment line loop count}

End of snippet

Helmut

Re:Intel Hex file conversion


Quote
Helmut Strickner <apo...@gil.com.au> wrote:
>Thanks to everyone who replied to my posting.
>In the meantime I have come up with a solution to my question.
>Here a snippet:
>   for c:= 1 to (Length(S)-1) do
>   {first string}
>   begin
>     if S[c] in ['A'..'F'] then
>     begin
>       i1 := Ord(S[c])-$37;
>       i2 := i1 shl 4;       {shift low nibble to high nibble}
>     end
>     else
>       if S[c] in ['0'..'9'] then
>       begin
>         i1 := Ord(S[c])-$30;
>         i2 := i1 shl 4;     {shift low nibble to high nibble}
>       end;
>      {second string}
>>> snip <<<
>End of snippet

Hi Helmut.

It seems that I didn't understand your problem, because if it's
the actually converting from hex to decimal, there is at least
two functions to do it: StrToInt ( if you prefer to use Exceptions )
& StrToIntDef ( if you want a non existing value ) and then with this
declaration:

Var
  HexVal: Array[ 0..24 ] Of Integer; { I use a Integer to set a value of
                                     -1 if wrong chars or end of indata }
  Cnt: Integer;
  Str: String[48];

... you can write:

Str := 'FD1300008002800602808175A80075818075D000C24B';

For Cnt := Low( HexVal ) To High( HexVal ) Do
  HexVal[ Cnt ] := StrToIntDef( '$' + Copy( Str, Cnt * 2, 2 ), -1 )

---------------------------------------------
  Ove Kjeldgaard
    !o_kj...@post4.tele.dk
      Remove the ! if you wish to e-mail me
---------------------------------------------

Other Threads