AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

From C to Delphi - some problem

Ein Thema von WojTec · begonnen am 25. Aug 2011 · letzter Beitrag vom 25. Aug 2011
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

From C to Delphi - some problem

  Alt 25. Aug 2011, 12:06
Delphi-Version: 2010
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?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.539 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: From C to Delphi - some problem

  Alt 25. Aug 2011, 12:09
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
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#3

Re: From C to Delphi - some problem

  Alt 25. Aug 2011, 12:16
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?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.539 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: From C to Delphi - some problem

  Alt 25. Aug 2011, 12:19
AFAIK yes. But maybe you are looking for Delphi-Referenz durchsuchenReverseString which is quite easier to use.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#5

Re: From C to Delphi - some problem

  Alt 25. Aug 2011, 12:21
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
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.539 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: From C to Delphi - some problem

  Alt 25. Aug 2011, 12:28
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.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#7

Re: From C to Delphi - some problem

  Alt 25. Aug 2011, 12:47
Delphi-Quellcode:
for I := 5 downto 0 do
  Caption := Caption + tabella[codeword[i] + 1]
;
Haaa Now working
Thank for your great help
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.539 Beiträge
 
Delphi 11 Alexandria
 
#8

AW: From C to Delphi - some problem

  Alt 25. Aug 2011, 12:48
No problem and sry for the wrong bracket-position
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:57 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