![]() // Getestet mit D4 unter WinME // Um
Interger-Werte in hexadezimale Form zu bringen, nutzt man in function AuchInToHex(Zahl: Int64; MindestStellen: Integer): string; begin result := Format('%.*x', [MindestStellen, Zahl]); end; // Beide Funktionen liefern Ziffern und Großbuchstaben in einem String zurück function IntToHexKG(Zahl, MindestStellen: integer; Klein: Boolean): string; begin setlength(result, 16); if not (MindestStellen in [1..16]) then MindestStellen := 1; setlength(result, wvsprintf(pchar(result), pchar('%.' + inttostr(MindestStellen) + chr(88 + ord(klein) * 32)), pchar(@Zahl))); end; // Falls man eine
alte Delphiversion hat
(oder auch aus
lauter Freude) function ToHex(Zahl: Int64; MindestStellen: integer): string; const Chars: array[0..15] of char = '0123456789ABCDEF'; var x: integer; p: ^byte; begin p := @Zahl; result := ''; for x := 1 to sizeof(Int64) do begin result := Chars[p^ shr 4] + Chars[p^ and 15] + result; inc(p); end; if not (MindestStellen in [1..16]) then MindestStellen := 1; for x := 1 to length(result) - MindestStellen do if result[x] <> '0' then begin result := copy(result, x, 16); exit; end; result := copy(result, length(result) - MindestStellen + 1, 16); end; // Braucht man keine "Mindeststellen", kann man so etwas machen: function Hex(zahl: DWord): string; var i: DWord; begin result := ''; repeat i := zahl mod 16; zahl := zahl div 16; case i of 0..9: result := chr(i + ord('0')) + result; 10..15: result := chr(i - 10 + ord('A')) + result; end; until zahl = 0; if length(result) mod 2 <> 0 then result := '0' + result; end;
//-------------------------------------------------------------------- function HexToInt(const st: string; var i: integer): boolean; var s: string; begin s := lowercase(st); s := stringreplace(s, '$', '', [rfreplaceall]); s := stringreplace(s, '#', '', [rfreplaceall]); s := stringreplace(s, 'h', '', [rfreplaceall]); try i := strtoint('$' + s); except result := false; exit; end; result := true; end; // Beispielaufruf procedure TForm1.Button3Click(Sender: TObject); var i: integer; s: string; begin s := '#FF00CC'; if HexToInt(s, i) then showmessage(inttostr(i)) // 16711884 else showmessage('Es liegt keine Hexadezimalzahl vor'); end;
// siehe auch:
Dezimalzahlen in andere Zahlensysteme konvertieren |
Zugriffe seit 6.9.2001 auf Delphi-Ecke