whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

41 Visitors Online


 
...convert a hexadecimal number into a Integer?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (23):  
     


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 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): stringoverload;

// Wandelt Wordwert aus Value in Hexadezimalwert (String) und
// trennt Hi- und Lo-Byte durch Splitter
function ToHex(Value: Word; Splitter: Char): stringoverload;

// Wandelt Longwordwert aus Value in Hexadezimalwert (String) und
// trennt Hi- und Lo-Word durch Splitter
function ToHex(Value: Cardinal; Splitter: Char): stringoverload;

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 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.

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners