Registriert seit: 17. Mai 2007
482 Beiträge
Delphi XE6 Professional
|
From C to Delphi - some problem
25. Aug 2011, 13: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?
|
|
Zitat
|