function IntToHex(Value: Integer; Digits: Integer): string; overload; function IntToHex(Value: Int64; Digits: Integer): string; overload;
Description
IntToHex converts a number into a string containing the number's hexadecimal (base 16) representation. Value is the number to convert. Digits indicates the minimum number of hexadecimal digits to return.
Function MyIntToHex(x:Integer):String; Const arr:String[16]='0123456789ABCDEF'; Var d,m:integer; temp: string; Begin temp := ''; result := ''; If x<0 then begin x := -x; result := '-'; end; repeat d := x div 16; m := x mod 16; temp := temp + arr[m+1]; x := d; until d <= 0; for d := length(temp) downto 1 do result := result + temp[d]; End;
procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := MyIntToHex(StrToInt(Edit1.Text)); end;
Her er den den anden vej: (den er lidt klampet, men den virker. Ret selv til)
Function MyHexToInt(x:String):LongInt; Const arr:String[16]='0123456789ABCDEF'; Var d,m:integer; sign : boolean; temp: string;
Function pow16(potens:integer): LongInt; Begin result := round(Exp(potens * ln(16))); End;
Begin temp := ''; result := 0; If length(x) > 0 then begin If x[1] = '-' then begin sign := true; if length(x) = 1 then exit else x := copy(x,2,255); end else sign := false; for d := length(x) downto 1 do temp := temp + x[d]; for d := 1 to length(temp) do begin m := pos(temp[d],arr)-1;; if m = -1 then exit; result := result+m*pow16(d-1); end; if sign then result := - result; end; End;
procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := IntToStr(MyHexToInt(Edit1.Text)); end;
Function pow16(potens:integer): Longint; Begin result := 1 shl (4*potens); End;
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.