mundodelphi

programacion en delphi

Convertir numeros de una base a otra

Funcion delphi conversora de decimal a binario, Conversión de Binario a Decimal en Delphi, Decimal codificado en Binario, Conversor binario/decimal/hexadecimal, Convertir Números Romanos a Numeros Decimales, Como convertir cifras numéricas a números Romanosen Delphi, Convertir Arábigos a Romanos con Delphi, Conversión a Números Romanos en Delphi, convertir numeros naturales a numeros romanos en Delphi, Convertir números arábigos en romanos en Delphi, Funcion para convertir numeros a numeros romanos en Delphi, Números Romanos Delphi, Conversión de números romanos con Delphi

Set de funciones para pasar de decimal a binario decimal octal, natural a romano y viceversa de binario a decimal, octal a decimal y romano a natural, para sistema hexadecimal delphi dispone de IntToHex y HexToInt

Listado de Funciones

  • IntToBin
  • BinToInt
  • IntToOctal
  • OctalToInt
  • IntToRoman
  • RomanToInt

Código de las funciones agrupadas

//----- Funciones para base binario
Function IntToBin ( Numero: LongInt; Digits: integer=8): string;
begin
result := StringOfChar ( '0', Digits ) ;
while Numero > 0 do begin
if ( Numero and 1 ) = 1 then
result [ digits ] := '1';
dec ( digits ) ;
Numero := Numero shr 1;
end;
end;

Function BinToInt(Numero: String): LongInt;
var i: Integer;
begin
Result:=0;
//quita los ceros delanteros
while Copy(Numero,1,1)='0' do Numero:=Copy(Numero,2,Length(Numero)-1) ;
//crea la conversion
for i:=Length(Numero) downto 1 do
if Copy(Numero,i,1)='1' then
Result:=Result+(1 shl (Length(Numero)-i)) ;
end;

//----- Funciones para base Octal
Function IntToOct(Numero: Longint; digits: Integer=8): string;
var
rest: Longint;
oct: string;
i: Integer;
begin
oct := '';
while Numero <> 0 do
begin
rest  := Numero mod 8;
Numero := Numero div 8;
oct := IntToStr(rest) + oct;
end;
for i := Length(oct) + 1 to digits do
oct := '0' + oct;
Result := oct;
end;

function OctToInt(Numero: string): Longint;
var
i: Integer;
int: Integer;
begin
int := 0;
for i := 1 to Length(Numero) do
begin
int := int * 8 + StrToInt(Copy(Numero, i, 1));
end;
Result := int;
end;

//----- Funciones para numeros Romanos
Function IntToRoman(Numero: LongInt): String;
const
Arabes: Array[1..13] of Integer = (1,4,5,9,10,40,50,90,100,400,500,900,1000) ;
Romanos: Array[1..13] of String = ('I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M') ;
var
i: Integer;
begin
for i := 13 downto 1 do
while (Numero >= Arabes[i]) do begin
Numero := Numero - Arabes[i];
Result := Result + Romanos[i];
end;
end;

function RomanToInt(const roman: string) : LongInt;
var
i: integer;
lastChar: Char;
begin
result := 0;
lastChar := ' ';
for i := Length(roman) downto 1 do
begin
case roman[i] of
'I': if (lastChar = 'X') or (lastChar = 'V') then
Dec(result) else Inc(result);
'V': Inc(result, 5);
'X': if (lastChar = 'C') or (lastChar = 'L') then
Dec(result, 10) else Inc(result, 10);
'L': Inc(result, 50);
'C': if (lastChar = 'M') or (lastChar = 'D') then
Dec(result, 100) else Inc(result, 100);
'D': Inc(result, 500);
'M': Inc(result, 1000);
end;
lastChar := roman[i];
end;
end;

Muestra de uso de la funciones

var
Numero_dec,Numero_dec1,Numero_dec2: Integer;
Numero_bin: String;
Numero_oct: String;
Numero_rom: String;
begin
Numero_dec := 123;
Numero_bin := IntToBin(Numero_dec);
Numero_oct := IntToOct(Numero_dec);
Numero_rom := IntToRoman(1999);

memo1.Lines.Clear;
memo1.Lines.add('Binario: ' + Numero_bin);
memo1.Lines.add('Octal: ' + Numero_oct);
memo1.Lines.add('Romano: ' + Numero_rom);

Numero_dec := BinToInt('01111011');
Numero_dec1 := OctToInt('173');
Numero_dec2 := RomanToInt('MCMXCIX');

memo1.Lines.add('Bin to decimal: ' + IntToStr(Numero_dec));
memo1.Lines.add('Oct to decimal: ' + IntToStr(Numero_dec1));
memo1.Lines.add('Roman to decimal: ' + IntToStr(Numero_dec2));
end;

Obtendremos

Binario: 01111011
Octal: 00000173
Romano: MCMXCIX
Bin to decimal: 123
Oct to decimal: 123
Roman to decimal: 1999

Fuentes: forosclubdelphi, delphitricks.com

Tagged as: ,

3 Comments

  1. Exelente, me ahorraste un gran tiempo y estaba buscando la funcio para la conversion octal

  2. Exelente, me ahorraste un gran tiempo y estaba buscando la funcion para la conversion octal

  3. esta super bn pero lo necesito hacer en un diagrama de flujo!!!!!!!!!

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Google Analytics Alternative