Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Cálculo de los dígitos IBAN (https://www.clubdelphi.com/foros/showthread.php?t=80864)

Troffed 10-05-2008 12:49:47

Cálculo de los dígitos IBAN
 
Requiere la librería DFF: http://www.delphiforfun.org/programs/Library/Default.htm

Código Delphi [-]
{ ——
Devuelve el código IBAN que corresponde con la cuenta y el país que se le pasa
Ver http://www.desarrolloweb.com/articulos/2484.php y http://www.sima.cat/chkiban.php
—— }
function ControlIBAN(const Cuenta, Pais: string): string;
var
  i, j: integer;
  m: int64;
  l: TInteger;
  t: string;
  s: string;

  function LetterToDigit(const C: Char): string;
  const
    a: char = ‘A’;
  var
    d: byte;
  begin
    result := C;
    if C in [‘A’..‘Z’] then
    begin
      d := (byte(C) – byte(a)) + 10;
      result := IntToStr(d);
    end;
  end;

begin
  l := TInteger.Create;
  try
    t := Cuenta + Pais + ‘00’;
    s := ‘’;
    j := Length(t);
    for i := 1 to j do
      s := s + LetterToDigit(t[i]);
    l.Assign(s);
    l.Modulo(97);
    l.ConvertToInt64(m);
    i := 98 – m;
    result := IntToStr(i);
    if i < 10 then result := ‘0’ + result;
  finally
    l.Free;
  end;
end;

function FormateaIBAN(const Cuenta, Pais: string): string;
begin
  result := Pais + ControlIBAN(Cuenta, Pais) + Cuenta;
end;

gerardus 13-05-2008 13:35:34

Gracias. Añado una rutina que verifica que un numero de cuenta sea valido.

const
Tabla_AlfaDigitos: array['A'..'Z'] of integer = (10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35);

// function Clean_IBAN
// Devuelve un numero de cuenta IBAN "limpio", sin 'IBAN', ni espacios, ni guiones...
function Clean_IBAN(Cuenta: string): string;
var
i: integer;
begin
result := '';
// Pasar a mayusculas i eliminar espacios
Cuenta := UpperCase(Trim(Cuenta));
// Eliminar posible 'IBAN'
if Copy(Cuenta, 1, 4) = 'IBAN' then
Delete(Cuenta, 1, 4);
// Pasar solo caracteres alfanumericos
for i := 1 to length(Cuenta) do
if Cuenta[i] in ['a'..'z', 'A'..'Z', '0'..'9'] then
result := result + Cuenta[i];
end;
// function check_iban
// devuelve true si el numero de cuenta que le pasamos es un numero de cuenta IBAN valido
function Check_IBAN(Cuenta: string): Boolean;
var
s, s2, s3: string;
i: integer;
AInt64: Int64;
AMod: Integer;
APos: Integer;
begin
if Trim(Cuenta)='' then
result := false
else
begin
try
// Pulir Cuenta.
Cuenta := Clean_IBAN(Cuenta);
// Mover 4 primeros digitos al final
s := Copy(Cuenta, 5, length(Cuenta) - 4) + Copy(Cuenta, 1, 4);
// Convertir letras a digitos
s2 := '';
for i := 1 to length(s) do
begin
if s[i] in ['A'..'Z'] then
s2 := s2 + FormatFloat('00', Tabla_AlfaDigitos[s[i]])
else
s2 := s2 + s[i];
end;
// Calcular resultado del numero mod 97.
// Bucle calculando mods
while length(s2) > 18 do
begin
s3 := Copy(s2, 1, 18);
Amod := StrToInt64(s3) mod 97;
s2 := IntToStr(AMod) + Copy(s2, 19, Length(s2) - 16);
end;
result := StrToInt64(s2) mod 97 = 1;
except
result := false;
end;
end;
end;

Troffed 13-05-2008 13:42:52

gerardus, me permito "repostear" tu mensaje utilizando las etiquetas CODE

Código:

const
Tabla_AlfaDigitos: array['A'..'Z'] of integer = (10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35);

// function Clean_IBAN
// Devuelve un numero de cuenta IBAN "limpio", sin 'IBAN', ni espacios, ni guiones...

function Clean_IBAN(Cuenta: string): string;
var
  i: integer;
begin
  result := '';
  // Pasar a mayusculas i eliminar espacios
  Cuenta := UpperCase(Trim(Cuenta));
  // Eliminar posible 'IBAN'
  if Copy(Cuenta, 1, 4) = 'IBAN' then
    Delete(Cuenta, 1, 4);
  // Pasar solo caracteres alfanumericos
  for i := 1 to length(Cuenta) do
  if Cuenta in ['a'..'z', 'A'..'Z', '0'..'9'] then
    result := result + Cuenta;
end;

// function check_iban
// devuelve true si el numero de cuenta que le pasamos es un numero de cuenta IBAN valido
function Check_IBAN(Cuenta: string): Boolean;
var
  s, s2, s3: string;
  i: integer;
  AInt64: Int64;
  AMod: Integer;
  APos: Integer;
begin
  if Trim(Cuenta)='' then
    result := false
  else
  begin
    try
      // Pulir Cuenta.
      Cuenta := Clean_IBAN(Cuenta);
      // Mover 4 primeros digitos al final
      s := Copy(Cuenta, 5, length(Cuenta) - 4) + Copy(Cuenta, 1, 4);
      // Convertir letras a digitos
      s2 := '';
      for i := 1 to length(s) do
      begin
        if s in ['A'..'Z'] then
          s2 := s2 + FormatFloat('00', Tabla_AlfaDigitos[s])
        else
          s2 := s2 + s;
      end;
      // Calcular resultado del numero mod 97.
      // Bucle calculando mods
      while length(s2) > 18 do
      begin
        s3 := Copy(s2, 1, 18);
        Amod := StrToInt64(s3) mod 97;
        s2 := IntToStr(AMod) + Copy(s2, 19, Length(s2) - 16);
      end;
      result := StrToInt64(s2) mod 97 = 1;
    except
      result := false;
    end;
  end;
end;

Espero que no te importe.

gerardus 13-05-2008 17:22:22

No, desde luego, mucho mejor así.

elmorsa 25-07-2020 02:28:36

Hola A todos,


Soy muy malo en el tema de caracteres a string y viceversa con mod y TIntger, assign, etc.


Alguien sabe si hay (y donde) un codigo en Delphi Basico que dado un pais y la antigua Cuenta me devuelva el IBAN completo?



Un ejemplo Ficticio:


Pais: ES, CCC: 2010 1234 19 2102365120 = ES7420101234192102365120


Desde ya, muchisimas gracias.

elmorsa 25-07-2020 02:58:18

He conseguido hacerlo en Delphi BASICO.



Como dije antes, no tengo ni idea, o sea que esto puede ser muy malo...


Funciones para transformar pais+CCC en IBAN:


Function Modulo97(const s: string): integer;
var
v, l : integer;
alpha : string;
number : longint;
begin
v := 1;
l := 9;
Result := 0;
alpha := '';

while (v <= Length(s)) do
begin
if (l > Length(s)) then
l := Length(s);
alpha := alpha + Copy(s, v, l);
number := StrToInt(alpha);
Result := number mod 97;
v := v + l;
alpha := IntToStr(Result);
l := 9 - Length(alpha);
end;
end;


Function ControlIBAN(const cuenta, Pais: string): string;
var
i, j: integer;
m: int64;
l: Integer;
t: string;
s: string;

function LetterToDigit(C: Char): string;
const
a: char = 'A';
var
d: byte;
begin
result := C;
if C in ['A'..'Z'] then
begin
d := (byte(C)-byte(a)) + 10;
result := IntToStr(d);
end;
end;

begin
try
t := Cuenta + Pais + '00';
s := '';
j := Length(t);
for i := 1 to j do
s := s + LetterToDigit(t[i]);
l:= Modulo97(s);
m:= int64(l);
i := 98 - m;
result := IntToStr(i);
if i < 10 then result := '0' + result;
except
result :='';
end;
end;


Function FormateaIBAN(const cuenta, Pais: string): string;
begin
result := Pais + ControlIBAN(Cuenta, Pais) + Cuenta;
end;

Casimiro Notevi 25-07-2020 09:04:44

Por favor, no olvides usar las etiquetas para código, ejemplo:



Casimiro Notevi 25-07-2020 09:05:37

Tu código con la etiqueta DELPHI:

Código Delphi [-]
Function Modulo97(const s: string): integer;
var
  v, l : integer;
  alpha : string;
  number : longint;
begin
  v := 1;
  l := 9;
  Result := 0;
  alpha := '';

  while (v <= Length(s)) do
  begin
     if (l > Length(s)) then
        l := Length(s);
     alpha := alpha + Copy(s, v, l);
     number := StrToInt(alpha);
     Result := number mod 97;
     v := v + l;
     alpha := IntToStr(Result);
     l := 9 - Length(alpha);
  end;
end;

Function ControlIBAN(const cuenta, Pais: string): string;
var
  i, j: integer;
  m: int64;
  l: Integer;
  t: string;
  s: string;

  function LetterToDigit(C: Char): string;
  const
    a: char = 'A';
  var
    d: byte;
  begin
    result := C;
    if C in ['A'..'Z'] then
    begin
      d := (byte(C)-byte(a)) + 10;
      result := IntToStr(d);
    end;
  end;

begin
  try
    t := Cuenta + Pais + '00';
    s := '';
    j := Length(t);
    for i := 1 to j do
      s := s + LetterToDigit(t[i]);
    l:= Modulo97(s);
    m:= int64(l);
    i := 98 - m;
    result := IntToStr(i);
    if i < 10 then result := '0' + result;
  except
    result :='';
  end;
end;

Function FormateaIBAN(const cuenta, Pais: string): string;
begin
  result := Pais + ControlIBAN(Cuenta, Pais) + Cuenta;
end;

elmorsa 26-07-2020 14:50:22

Perdón No sabía que existía esta formalidad... Es muy buena... Gracias por informarme.

Casimiro Notevi 26-07-2020 18:08:27

Pues ya lo sabes :D


La franja horaria es GMT +2. Ahora son las 14:59:28.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi