Translate short C source to Delphi

umulig
umulig used Ask the Experts™
on
#include <stdio.h>
 
int main(int argc, char **argv)
{
   unsigned short chksum, x, y;
 
   FILE *f;
 
   int i;
 
   f = fopen(argv[1],"rb");
 
   if (f)
   {
      chksum = 0;
 
      for(i=0; i<0x7AFFE; i += 2) // Word Wide Simple Sum
      {
         x = fgetc(f);
         y = fgetc(f);
 
         chksum += ((x << 8) + y);
      }
 
      printf("%04X Compute\n",chksum);
 
      x = fgetc(f);
      y = fgetc(f);
 
      printf("%04X Actual\n",((x << 8) + y));
 
 
      x = fgetc(f);
      y = fgetc(f);
      x = fgetc(f);
      y = fgetc(f);
 
 
 
      chksum = 0;
 
      for(i=0; i<0x1FFFE; i += 2) // Word Wide Simple Sum
      {
         x = fgetc(f);
         y = fgetc(f);
 
         chksum += ((x << 8) + y);
      }
 
      printf("%04X Compute\n",chksum);
 
      x = fgetc(f);
      y = fgetc(f);
 
      printf("%04X Actual\n",((x << 8) + y));
 
 
 
      fclose(f);
   }
 
   return(1);
}


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I'm not sure this will work as I cannot test it, but if it isn't right, at least it is near to right .... I created it as a console project, but you can use the function f in a GUI project as well .... : ) Have fun ....

Fernando - Brasil


program Project2;

uses SysUtils;
{$APPTYPE CONSOLE}

function f(s: string): integer;
var
  chksum, x, y: shortint;
  i: integer;
  f: file;
begin
  Assign(f, s);
  {$I-}
  Reset(f, sizeof(shortint));
  {$I+}
  if ioresult = 0 then
  begin
    chksum := 0;
    i := 0;
    while i < $7AFFE do
    begin
      blockread(f, x, 1);
      blockread(f, y, 1);
      chksum := chksum + ((x shl 8) + y);
      i := i+2;
    end;

    writeln(Format('%04X Compute',[chksum]));

    blockread(f, x, 1);
    blockread(f, y, 1);

    writeln(Format('%04X Actual',[(x shl 8) + y]));


    blockread(f, x, 1);
    blockread(f, y, 1);
    blockread(f, x, 1);
    blockread(f, y, 1);



    chksum := 0;
    i := 0;
    while (i<$1FFFE) do
    begin
      blockread(f, x, 1);
      blockread(f, y, 1);
      chksum := chksum + ((x shl 8) + y);
      i := i+2;
    end;

    writeln(Format('%04X Compute',[chksum]));

    blockread(f, x, 1);
    blockread(f, y, 1);

    writeln(Format('%04X Actual',[((x shl 8) + y)]));

    Close(f);
  end;
  result := 1;
end;

begin
  f(paramstr(1));
end.

Author

Commented:
Orginal proggi returns
56D8 Compute
56D8 Actual
C635 Compute
C635 Actual

While the code abow returns
FFFFFFD8 Compute
55D8 Actual
  35 Compute
FFFFC635 Actual

Close , but not correct as far as i can see.
Could you post some input samples for me to test?
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
I've sendt you a mail with the file i've tested with.

program Translated;
{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  chksum, x, y: Byte;
  F: file of byte;
  i: Integer;
begin
  AssignFile(F, ParamStr(1));
  Reset(F);
  chksum := 0;
  i := 0;
  while (i < $7affe) do
  begin
    Read(f, x);
    Read(f, y);
    chksum := chksum + ((x shl 8) + y);
    Inc(i, 2);
  end;

  WriteLn(IntToHex(chksum, 4), ' Compute');

  Read(f, x);
  Read(f, y);
  WriteLn(IntToHex((x shl 8) + y), 4), ' Actual');

  Read(f, x);
  Read(f, y);
  Read(f, x);
  Read(f, y);

  chksum := 0;
  i := 0;

  while (i < $1fffe) do
  begin
    Read(f, x);
    Read(f, y);
    chksum := chksum + ((x shl 8) + y);
    Inc(i, 2);
  end;

  WriteLn(IntToHex(chksum, 4), ' Compute');

  Read(f, x);
  Read(f, y);
  WriteLn(IntToHex((x shl 8) + y), 4), ' Actual');

  CloseFile(f);
end.
Dragon: yes, guy, I forgot short in C++ is one byte long ; ) It seems you got it
Ops, ignore my previous comment, shortint is one byte long too ....
I made 2 little changes and now it is working .... chksum must be stored in a word, because it is 16 bits long, and I had to change shortint to byte, once shortint is signed 8 bits, and byte is unsigned ..... There goes the code

program Working;

uses SysUtils;
{$APPTYPE CONSOLE}

function f(s: string): integer;
var
 x, y: byte;             // byte is unsigned 8 bits, shortint is signed 8 bits
 chksum: word;            // chksum must be 16 bits
 i: integer;
 f: file;
begin
 Assign(f, s);
 {$I-}
 Reset(f, sizeof(byte));
 {$I+}
 if ioresult = 0 then
 begin
   chksum := 0;
   i := 0;
   while i < $7AFFE do
   begin
     blockread(f, x, 1);
     blockread(f, y, 1);
     chksum := chksum + ((x shl 8) + y);
     i := i+2;
   end;

   writeln(Format('%04X Compute',[chksum]));

   blockread(f, x, 1);
   blockread(f, y, 1);

   writeln(Format('%04X Actual',[(x shl 8) + y]));


   blockread(f, x, 1);
   blockread(f, y, 1);
   blockread(f, x, 1);
   blockread(f, y, 1);



   chksum := 0;
   i := 0;
   while (i<$1FFFE) do
   begin
     blockread(f, x, 1);
     blockread(f, y, 1);
     chksum := chksum + ((x shl 8) + y);
     i := i+2;
   end;

   writeln(Format('%04X Compute',[chksum]));

   blockread(f, x, 1);
   blockread(f, y, 1);

   writeln(Format('%04X Actual',[((x shl 8) + y)]));

   Close(f);
 end;
 result := 1;
end;

begin
 f(paramstr(1));
end.

Author

Commented:
Just want I needed ;)

Thx a lot

Umulig

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial