elmago00,
Cita:
Empezado por elmago00
...¿Cómo convertir un Integer a Hexadecimal?...El valor Integer que utilizo es 256312456892254...Como hago para generar el mismo hexadecimal 082A6513426598224500000000000000...
|
Revisa este código:
Código Delphi
[-]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DecToHex(Dec : UInt64): String;
var
Hex : String;
Rst : UInt64;
begin
repeat
Rst := Dec Mod 16;
if Rst = 10 Then Hex := 'A';
if Rst = 11 Then Hex := 'B';
if Rst = 12 Then Hex := 'C';
if Rst = 13 Then Hex := 'D';
if Rst = 14 Then Hex := 'E';
if Rst = 15 Then Hex := 'F';
if Rst in [0..9] then Hex := IntToStr(Rst);
Result := Result + Hex;
Dec := Dec shr 4;
until (Dec = 0);
Result := ReverseString(Result);
end;
function HexToDec(Hex : String): UInt64;
var
i : Integer;
M : UInt64;
begin
Result:=0;
M := 1;
Hex := AnsiUpperCase(Hex);
for i := Length(Hex) downto 1 do
begin
case Hex[i] of
'1'..'9' : Result := Result + (Ord(Hex[i]) - Ord('0')) * M;
'A'..'F' : Result := Result + (Ord(Hex[i]) - Ord('A') + 10) * M;
end;
M := M shl 4;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := DecToHex(StrToInt64(Edit1.Text));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit2.Text := IntToStr(HexToDec(Edit1.Text));
end;
end.
El código anterior en Delphi 2010 bajo Windows 7 Professional x32,
convierte números de Decimal a Hexadecimal y viceversa, como se puede ver en la siguiente imagen:
Notas:
1- El rango de números enteros que puede manejar el código propuesto es de
0 a 2^64 - 1 (Tipo UInt64).
2- El número hexadecimal
082A6513426598224500000000000000 no es el equivalente del número decimal
256312456892254, como se puede ver en la siguiente imagen :
Tomado de :
Conversor binario/decimal/hexadecimal
3- Para números tan grandes (Como el hexadecimal
082A6513426598224500000000000000), se requiere de una librería aritmética de precisión arbitraria, te sugiero consultar:
Espero sea útil
Nelson.