LUHN MOD N

Delphi

19/06/2018

Algum exemplo em delphi dessa função?
Tiago

Tiago

Curtidas 0

Melhor post

Raimundo Pereira

Raimundo Pereira

19/06/2018

Exemplo :

Migrando o Código Delphi para o Mobile from Desktop | Use seqüências de caracteres com base em 0

Para que o código funcione de maneira que você não precise lidar com as diferenças do ZBS, você pode:

usar {$ZEROBASEDSTRINGS OFF}para voltar a indexação baseada em 1 em dispositivos móveis

{$IFDEF NEXTGEN}
  {$ZEROBASEDSTRINGS OFF}
{$ENDIF}

function Luhn(Code: string): Boolean;
var
  i, sum: integer;
  temp: byte;
begin
  { calcula o algorítimo luhn, usado no iccid }
  sum := 0;
  for i := Length(Code) downto 1 do begin  // Run the characters backwards
    temp := byte(Ord(Code[i]))-48;  // Convert from ASCII to byte
    if (Length(Code)-i) mod 2 = 0 then
      sum := sum + temp             // Odd characters just add
    else if temp < 5 then
      sum := sum + (2*temp)         // Even characters add double
    else
      sum := sum + (2*temp) - 9;    // or sum the digits of the doubling
  end;
  Result := sum mod 10 = 0;         // Return true if sum ends in a 0
  if Result then
    Toast(Code+#13+'True')
  else
    Toast(Code+#13+'False');
end;

{$IFDEF NEXTGEN}
  {$ZEROBASEDSTRINGS ON}
{$ENDIF}
use as funções Low(String)e do High(String)sistema:

function Luhn(Code: string): Boolean;
var
  i, sum: integer;
  temp: byte;
begin
  { calcula o algorítimo luhn, usado no iccid }
  sum := 0;
  for i := High(Code) downto Low(Code) do begin  // Run the characters backwards
    temp := byte(Ord(Code[i]))-48;  // Convert from ASCII to byte
    if (High(Code)-i) mod 2 = 0 then
      sum := sum + temp             // Odd characters just add
    else if temp < 5 then
      sum := sum + (2*temp)         // Even characters add double
    else
      sum := sum + (2*temp) - 9;    // or sum the digits of the doubling
  end;
  Result := sum mod 10 = 0;         // Return true if sum ends in a 0
  if Result then
    Toast(Code+#13+'True')
  else
    Toast(Code+#13+'False');
end;
use a TStringHelperclasse auxiliar:

uses
  ..., SysUtils;

function Luhn(Code: string): Boolean;
var
  i, sum: integer;
  temp: byte;
begin
  { calcula o algorítimo luhn, usado no iccid }
  sum := 0;
  for i := Code.Length-1 downto 0 do begin  // Run the characters backwards
    temp := byte(Ord(Code.Chars[i]))-48;  // Convert from ASCII to byte
    if (Code.Length-1-i) mod 2 = 0 then
      sum := sum + temp             // Odd characters just add
    else if temp < 5 then
      sum := sum + (2*temp)         // Even characters add double
    else
      sum := sum + (2*temp) - 9;    // or sum the digits of the doubling
  end;
  Result := sum mod 10 = 0;         // Return true if sum ends in a 0
  if Result then
    Toast(Code+#13+'True')
  else
    Toast(Code+#13+'False');
end;
GOSTEI 2

Mais Respostas

Tiago

Tiago

19/06/2018

Obrigado pela resposta. Não cheguei a usar pois consegui resolver o problema do meu código.
GOSTEI 0
POSTAR