![]() |
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:
Now what I did:
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; }
Delphi-Quellcode:
Up to ShowMessage() is ok, but next is some bug, I think with indexes. Could you help find this bug?
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; 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? |
AW: From C to Delphi - some problem
Your arrays go from 1 to 5, so code like this won' t do it:
Zitat:
|
Re: From C to Delphi - some problem
So, C for working 5..1 or 5..0?
Code:
Declaration
for(i = 5; i >= 0; i--)
Code:
mean
int codeword[6];
Code:
or what?
array [0..5]
|
AW: From C to Delphi - some problem
AFAIK yes. But maybe you are looking for
![]() |
Re: From C to Delphi - some problem
Delphi-Quellcode:
But result is wrong :(
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; |
AW: From C to Delphi - some problem
Try this:
Delphi-Quellcode:
Strings in Delphi start with index 1, not 0 like in C.
for i := 5 downto 0 do
risultante[5 - i] := tabella[codeword[i + 1]]; |
Re: From C to Delphi - some problem
Delphi-Quellcode:
Haaa :-D Now working :-D
for I := 5 downto 0 do
Caption := Caption + tabella[codeword[i] + 1] ; Thank for your great help :-D |
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 15:55 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz