Link to home
Start Free TrialLog in
Avatar of umulig
umulig

asked on

Translate short C source to Delphi

#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);
}


Avatar of knightmad
knightmad

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.
Avatar of umulig

ASKER

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?
Avatar of umulig

ASKER

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 ....
ASKER CERTIFIED SOLUTION
Avatar of knightmad
knightmad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of umulig

ASKER

Just want I needed ;)

Thx a lot

Umulig