Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Convert a hexadecimal number into a integer

function HexToInt(s: string): Longword; var b: Byte; c: Char; begin Result := 0; s := UpperCase(s); for b := 1 to Length(s) do begin Result := Result * 16; c := s[b]; case c of '0'..'9': Inc(Result, Ord(c) - Ord('0')); 'A'..'F': Inc(Result, Ord(c) - Ord('A') + 10); else raise EConvertError.Create('No Hex-Number'); end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(IntToStr(StrToHex('AAF1'))); // > 43761 end; {*************************************************} procedure HexToInt(s: string): Integer; begin Result := StrToInt('$' + s); end; {*************************************************} function HexToInt(strHexValue : string) : Integer; var c,l : integer; begin Val(strHexValue, l, c); Result := l; end; procedure TForm1.Button2Click(Sender: TObject); begin ShowMessage(IntToStr(HexToInt('$EAD1'))); end; {***************************************************} unit HexConvert; { by André Fritzsche } interface uses SysUtils; // Wandelt Hexadezimalwert in Value zu Zahl function HexTo(Value: string): Longword; // Wandelt Bytewert aus Value in Hexadezimalwert (String) function ToHex(Value: Byte): string; overload; // Wandelt Wordwert aus Value in Hexadezimalwert (String) und // trennt Hi- und Lo-Byte durch Splitter function ToHex(Value: Word; Splitter: Char): string; overload; // Wandelt Longwordwert aus Value in Hexadezimalwert (String) und // trennt Hi- und Lo-Word durch Splitter function ToHex(Value: Cardinal; Splitter: Char): string; overload; implementation const HexTbl: string = '0123456789ABCDEF'; {------------------------------------------------------------------------------} function HexTo(Value: string): Longword; var intX, PosCnt: Byte; zwVal: Integer; begin Result := 0; PosCnt := 0; //Halb-Byte-Position for intX := Length(Value) - 1 downto 0 do begin //alle Zeichen von links durchlaufen zwVal := Pos(UpperCase(Value[intX + 1]), HexTbl) - 1; //prüfen, ob Zeichen aus Value if zwVal >= 0 then begin //in HexTbl vorkommt und Position zu zwVal Result := Result + (zwVal shl (4 * PosCnt)); //zwVal 4*Halbbyteposition Bits nach links verschieben Inc(PosCnt); //Halb-Byte Position erhöhen end; end; end; {------------------------------------------------------------------------------} function ToHex(Value: Byte): string; var zwVal: Byte; begin zwVal := (Value and $0F); //erstes (Lo)-Byte von Value maskieren Result := HexTbl[zwVal + 1]; //aus HexTbl zu Result zwVal := (Value and $F0) shr 4; //zweites (Hi)-Byte von Value maskieren und 4Bits nach rechts verschieben Result := HexTbl[zwVal + 1] + Result; //aus HexTbl vor Result end; {------------------------------------------------------------------------------} function ToHex(Value: Word; Splitter: Char): string; begin Result := ToHex(Byte(Lo(Value))); //LoByte umwandeln, zu Result Result := ToHex(Byte(Hi(Value))) + Splitter + Result; //HiByte umwandeln, zu Result, Trennzeichen davorsetzen end; {------------------------------------------------------------------------------} function ToHex(Value: Cardinal; Splitter: Char): string; var zwVal: Word; begin zwVal := Value and $0000FFFF; //loword maskieren Result := ToHex(Word(zwVal)); //umwandeln und zu Result zwVal := (Value and $FFFF0000) shr 16; //hiword maskieren Result := ToHex(Word(zwVal)) + Splitter + Result; //umwandeln und zu Result, Trennzeichen davorsetzen end; {------------------------------------------------------------------------------} end.