Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > Trucos
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Los mejores trucos

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 10-05-2008
Troffed Troffed is offline
Miembro
 
Registrado: mar 2004
Posts: 51
Poder: 21
Troffed Va por buen camino
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;
Responder Con Cita
  #2  
Antiguo 13-05-2008
gerardus gerardus is offline
Miembro
 
Registrado: dic 2007
Posts: 43
Poder: 0
gerardus Va por buen camino
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;
Responder Con Cita
  #3  
Antiguo 13-05-2008
Troffed Troffed is offline
Miembro
 
Registrado: mar 2004
Posts: 51
Poder: 21
Troffed Va por buen camino
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.
Responder Con Cita
  #4  
Antiguo 13-05-2008
gerardus gerardus is offline
Miembro
 
Registrado: dic 2007
Posts: 43
Poder: 0
gerardus Va por buen camino
No, desde luego, mucho mejor así.
Responder Con Cita
  #5  
Antiguo 25-07-2020
elmorsa elmorsa is offline
Registrado
 
Registrado: jul 2020
Posts: 5
Poder: 0
elmorsa Va por buen camino
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.
Responder Con Cita
  #6  
Antiguo 25-07-2020
elmorsa elmorsa is offline
Registrado
 
Registrado: jul 2020
Posts: 5
Poder: 0
elmorsa Va por buen camino
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;
Responder Con Cita
  #7  
Antiguo 25-07-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.021
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Por favor, no olvides usar las etiquetas para código, ejemplo:


Responder Con Cita
  #8  
Antiguo 25-07-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.021
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
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;
Responder Con Cita
  #9  
Antiguo 26-07-2020
elmorsa elmorsa is offline
Registrado
 
Registrado: jul 2020
Posts: 5
Poder: 0
elmorsa Va por buen camino
Perdón No sabía que existía esta formalidad... Es muy buena... Gracias por informarme.

Última edición por Casimiro Notevi fecha: 26-07-2020 a las 19:08:14.
Responder Con Cita
  #10  
Antiguo 26-07-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.021
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Pues ya lo sabes
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 23:10:29.


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
Copyright 1996-2007 Club Delphi