function GenerarIBAN(Pais, Cuenta: string): string;
function EsAlfanumerico(Caracter: Char): boolean;
begin
Result := (Caracter in ['A'..'Z']) or (Caracter in ['a'..'z']);
end;
function EsNumerico(Caracter: Char): boolean;
begin
Result := (Caracter in ['0'..'9']);
end;
var
cAux,
AuxCuenta: string;
i: Integer;
auxTemp: Extended;
begin
Result := '';
Cuenta := Trim(Cuenta);
if (Cuenta = '') or (Length(Cuenta) > 34) then
Exit
else
begin
// Fase 1: Nos aseguramos que solo contiene Letras y Numeros
Cuenta := UpperCase(Cuenta);
cAux := '';
for i := 1 to Length(Cuenta) do
if (EsAlfanumerico(Cuenta[i]) or EsNumerico(Cuenta[i])) then
cAux := cAux + AnsiString(Cuenta[i]);
Cuenta := cAux;
AuxCuenta := Cuenta;
// Fase 2: Se comprueba si ya es un codigo IBAN, y si no lo es, se añade el PAIS
if (EsAlfanumerico(Cuenta[1]) and EsAlfanumerico(Cuenta[2])) then // Es IBAN
begin
if (EsAlfanumerico(Cuenta[3]) or EsAlfanumerico(Cuenta[4])) then
Exit;
Pais := Copy(Cuenta, 1, 2);//Cuenta.SubString(1, 2);
Cuenta := Copy(Cuenta, 5, Length(Cuenta));//Cuenta.SubString(5, Length(Cuenta)) + Cuenta.SubString(1, 2) + '00';
AuxCuenta := Cuenta;
end
else
begin
//y si no lo es, se añade el PAIS
if (Trim(Pais) = '') then
Pais := 'ES';
end;
Cuenta := Cuenta + Pais + '00';
// Fase 3: Se convierten las letras del pais en sus numeros equivalentes:
//A=10, B=11, C=12 ... Z=35
cAux := '';
for i := 1 to Length(Cuenta) do
begin
if (EsAlfanumerico(Cuenta[i])) then
cAux := cAux + FormatFloat('00', Ord(Cuenta[i]) - 55)//Cuenta[i - 1] - 55)
else
cAux := cAux + Copy(Cuenta, i, 1);
end;
Cuenta := cAux;
// Fase 4: Dividimos por 97
auxTemp := StrToInt(Copy(Cuenta, 1, 9)) mod 97;
cAux := FormatFloat('0', auxTemp);//FormatFloat("0", StrToInt(Cuenta.SubString(1, 9)) % 97);
Cuenta := Copy(Cuenta, 10, Length(Cuenta));//Cuenta.SubString(10, Cuenta.Length());
while (Trim(Cuenta) <> '') do
begin
if (StrToInt(cAux) < 10) then
begin
cAux := cAux + Copy(Cuenta, 1, 8);//Cuenta.SubString(1, 8);
Cuenta := Copy(Cuenta, 9, Length(Cuenta));//Cuenta.SubString(9, Cuenta.Length());
end
else
begin
cAux := cAux + Copy(Cuenta, 1, 7);//Cuenta.SubString(1, 7);
Cuenta := Copy(Cuenta, 8, Length(Cuenta));//Cuenta.SubString(8, Cuenta.Length());
end;
auxTemp := StrToInt(cAux) mod 97;
cAux := FormatFloat('0', auxTemp);
end;
// Fase 5: Devolvemos el IBAN completo con sus digitos de control.
// Se puede cambiar para devolver solo el digito de control, o lo que se quiera.
Result := Pais + FormatFloat('00', 98 - StrToInt(cAux)) + AuxCuenta;
end;
end;