Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi From C to Delphi - some problem (https://www.delphipraxis.net/162501-c-delphi-some-problem.html)

WojTec 25. Aug 2011 12:06

Delphi-Version: 2010

From C to Delphi - some problem
 
I want to train translate form C to Delphi. Here is function in C (from Zint project):

Code:
int code32(struct zint_symbol *symbol, unsigned char source[], int length)
{ /* Italian Pharmacode */
   int i, zeroes, error_number, checksum, checkpart, checkdigit;
   char localstr[10], risultante[7];
   long int pharmacode, remainder, devisor;
   int codeword[6];
   char tabella[34];
   
   /* Validate the input */
   if(length > 8) {
      strcpy(symbol->errtxt, "Input too long");
      return ZERROR_TOO_LONG;
   }
   error_number = is_sane(NEON, source, length);
   if(error_number == ZERROR_INVALID_DATA) {
      strcpy(symbol->errtxt, "Invalid characters in data");
      return error_number;
   }
   
   /* Add leading zeros as required */
   zeroes = 8 - length;
   memset(localstr, '0', zeroes);
   strcpy(localstr + zeroes, (char*)source);
   
   /* Calculate the check digit */
   checksum = 0;
   checkpart = 0;
   for(i = 0; i < 4; i++) {
      checkpart = ctoi(localstr[i * 2]);
      checksum += checkpart;
      checkpart = 2 * (ctoi(localstr[(i * 2) + 1]));
      if(checkpart >= 10) {
         checksum += (checkpart - 10) + 1;
      } else {
         checksum += checkpart;
      }
   }
   
   /* Add check digit to data string */
   checkdigit = checksum % 10;
   localstr[8] = itoc(checkdigit);
   localstr[9] = '\0';
   
   /* Convert string into an integer value */
   pharmacode = atoi(localstr);

   /* Convert from decimal to base-32 */
   devisor = 33554432;
   for(i = 5; i >= 0; i--) {
      codeword[i] = pharmacode / devisor;
      remainder = pharmacode % devisor;
      pharmacode = remainder;
      devisor /= 32;
   }
   
   /* Look up values in 'Tabella di conversione' */
   strcpy(tabella, "0123456789BCDFGHJKLMNPQRSTUVWXYZ");
   for(i = 5; i >= 0; i--) {
      risultante[5 - i] = tabella[codeword[i]];
   }
   risultante[6] = '\0';
   /* Plot the barcode using Code 39 */
   error_number = c39(symbol, (unsigned char*)risultante, strlen(risultante));
   if(error_number != 0) { return error_number; }
   
   /* Override the normal text output with the Pharmacode number */
   ustrcpy(symbol->text, (unsigned char*)"A");
   uconcat(symbol->text, (unsigned char*)localstr);
   
   return error_number;
}
Now what I did:

Delphi-Quellcode:
const
  tabella = '0123456789BCDFGHJKLMNPQRSTUVWXYZ';
var
  localstr: string;
  checksum, checkpart, I, pharmacode, devisor, remainder: Integer;
  codeword: array [1..5] of Integer;
  risultante: array [1..5] of Char;
begin
  localstr := '84560146';

   checksum := 0;
   checkpart := 0;

  for I := 0 to 3 do
  begin
    Inc(checksum, StrToInt(localstr[i+1 * 2]));
    checkpart := 2 * (StrToInt(localstr[(i * 2) + 2]));
      if checkpart >= 10 then
      Inc(checksum, (checkpart - 10) + 2)
      else
      Inc(checksum, checkpart);
      ;
  end;

  localstr := localstr + IntToStr(checksum mod 10);
  ShowMessage(localstr);

  pharmacode := StrToInt(localstr);
  devisor := 33554432;
   for I := 5 downto 1 do
  begin
      codeword[i] := pharmacode div devisor;
      remainder := pharmacode mod devisor;
      pharmacode := remainder;
      devisor := devisor div 32;
   end;

   for i := 5 downto 1 do
      risultante[5 - i] := tabella[codeword[i]];
   ;

  for I := 1 to 5 do
    Caption := Caption + risultante[I]
  ;
end;
Up to ShowMessage() is ok, but next is some bug, I think with indexes. Could you help find this bug?

Also I'm not satisfacted with my version of first for loop, because I added +2 (in original is +1), could you suggest better version, please?

DeddyH 25. Aug 2011 12:09

AW: From C to Delphi - some problem
 
Your arrays go from 1 to 5, so code like this won' t do it:
Zitat:

Delphi-Quellcode:
for i := 5 downto 1 do
      risultante[5 - i] := tabella[codeword[i]]; //5 - 5 = 0, there is no index 0 in risultante


WojTec 25. Aug 2011 12:16

Re: From C to Delphi - some problem
 
So, C for working 5..1 or 5..0?
Code:
for(i = 5; i >= 0; i--)
Declaration
Code:
int codeword[6];
mean
Code:
array [0..5]
or what?

DeddyH 25. Aug 2011 12:19

AW: From C to Delphi - some problem
 
AFAIK yes. But maybe you are looking for Delphi-Referenz durchsuchenReverseString which is quite easier to use.

WojTec 25. Aug 2011 12:21

Re: From C to Delphi - some problem
 
Delphi-Quellcode:
const
  tabella = '0123456789BCDFGHJKLMNPQRSTUVWXYZ';
var
  localstr: string;
  {...}
  codeword: array [0..5] of Integer;
  risultante: array [0..5] of Char;
begin
  {...}

  pharmacode := StrToInt(localstr); // = let's say 845601463
  devisor := 33554432;
   for I := 5 downto 0 do
  begin
      codeword[i] := pharmacode div devisor;
      remainder := pharmacode mod devisor;
      pharmacode := remainder;
      devisor := devisor div 32;
   end;

   for i := 5 downto 0 do
      risultante[5 - i] := tabella[codeword[i]];
   ;

  Caption := '';
  for I := 0 to 5 do
    Caption := Caption + risultante[I]
  ;
end;
But result is wrong :(

DeddyH 25. Aug 2011 12:28

AW: From C to Delphi - some problem
 
Try this:
Delphi-Quellcode:
for i := 5 downto 0 do
  risultante[5 - i] := tabella[codeword[i + 1]];
Strings in Delphi start with index 1, not 0 like in C.

WojTec 25. Aug 2011 12:47

Re: From C to Delphi - some problem
 
Delphi-Quellcode:
for I := 5 downto 0 do
  Caption := Caption + tabella[codeword[i] + 1]
;
Haaa :-D Now working :-D
Thank for your great help :-D

DeddyH 25. Aug 2011 12:48

AW: From C to Delphi - some problem
 
No problem and sry for the wrong bracket-position :oops:


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:42 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz