Mega Code Archive

 
Categories / Delphi / Examples
 

Hextoint

Question/Problem/Abstract: Delphi kindly gave us IntToHex, but forgot to supply it's counterpart HexToInt. (IntToHex converts a number into a string containing the number's hexadecimal (base 16) representation) Also no binary functions are in the sysutils unit, IntToBin and BinToInt. Here's how to implement the missing functionality. Uses Delphi 5 Int64 type but can be modified for all Delphi versions. *************************************** { ======================================= } { Convert a HexString value to an Int64 } { Note : Last Char can be 'H' for Hex } { eg. '00123h' or '00123H' } { 0 will be returned if invalid HexString } { ======================================= } function HexToInt(HexStr : string) : Int64; var RetVar : Int64; i : byte; begin HexStr := UpperCase(HexStr); if HexStr[length(HexStr)] = 'H' then Delete(HexStr,length(HexStr),1); RetVar := 0; for i := 1 to length(HexStr) do begin RetVar := RetVar shl 4; if HexStr[i] in ['0'..'9'] then RetVar := RetVar + (byte(HexStr[i]) - 48) else if HexStr[i] in ['A'..'F'] then RetVar := RetVar + (byte(HexStr[i]) - 55) else begin Retvar := 0; break; end; end; Result := RetVar; end; { ============================================== } { Convert an Int64 value to a binary string } { NumBits can be 64,32,16,8 to indicate the } { return value is to be Int64,DWord,Word } { or Byte respectively (default = 64) } { NumBits normally are only required for } { negative input values } { ============================================== } function IntToBin(IValue : Int64; NumBits : word = 64) : string; var RetVar : string; i,ILen : byte; begin RetVar := ''; case NumBits of 32 : IValue := dword(IValue); 16 : IValue := word(IValue); 8 : IValue := byte(IValue); end; while IValue <> 0 do begin Retvar := char(48 + (IValue and 1)) + RetVar; IValue := IValue shr 1; end; if RetVar = '' then Retvar := '0'; Result := RetVar; end; { ============================================== } { Convert a bit binary string to an Int64 value } { Note : Last Char can be 'B' for Binary } { eg. '001011b' or '001011B' } { 0 will be returned if invalid BinaryString } { ============================================== } function BinToInt(BinStr : string) : Int64; var i : byte; RetVar : Int64; begin BinStr := UpperCase(BinStr); if BinStr[length(BinStr)] = 'B' then Delete(BinStr,length(BinStr),1); RetVar := 0; for i := 1 to length(BinStr) do begin if not (BinStr[i] in ['0','1']) then begin RetVar := 0; Break; end; RetVar := (RetVar shl 1) + (byte(BinStr[i]) and 1) ; end; Result := RetVar; end; *************************************** Comments to this article Write a new comment Int2Bin Jan Horn (Sep 28 2000 1:58AM) Converts a 32bit integer to a binary number. function Base10(Base2:Integer) : Integer; assembler; asm cmp eax,100000000 // check upper limit jb @1 // ok mov eax,-1 // error flag jmp @exit // exit with -1 @1: push ebx // save registers push esi xor esi,esi // result = 0 mov ebx,10 // diveder base 10 mov ecx,8 // 8 nibbles (10^8-1) @2: mov edx,0 // clear remainder div ebx // eax DIV 10, edx mod 10 add esi,edx // result = result + remainder[I] ror esi,4 // shift nibble loop @2 // loop for all 8 nibbles mov eax,esi // function result pop esi // restore registers pop ebx @exit: end; *************************************** Here is another small HexToInt function function HexToInt(HexString : String) : Integer; var s : string; begin s := '$' + HexString; result := StrToInt(a); end; *************************************** {---------------------------------------------------------------------} Function Hex2Int(S : String) : LongInt; Const HexChars : String[16] = '0123456789ABCDEF'; Var I : Integer; Begin Result := 0; For I := 1 to Length(S) do Begin Result := Result shl 4; Result := Result or (Pos(S[I],HexChars) - 1); End; End; {---------------------------------------------------------------------} Function HexStr(LI : LongInt; Cnt : Byte) : String; Const HexCharArr : Array[0..$F] of Char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); Var I : Integer; Begin For I := 0 to Cnt-1 do Result := HexCharArr[ ((LI shr (4 * I)) and $000F) ] + Result; End; {---------------------------------------------------------------------} *************************************** function BinToInt (BinStr: string) : Int64; var I, Len: integer; const Mask: int64 = 1; begin Result := 0; Len := Length (BinStr); if (UpperCase (BinStr [Len]) = 'B') then begin Dec (Len); end; {if} for I := 1 to Len do begin case BinStr [I] of '0': begin {do nothing} end; '1': begin Result := Result OR (Mask SHL (Len - I)) end; else begin raise Exception.Create ('Wrong binary string'); end; end; {case} end; {for I} end;