Ӕ Central

Welcome to the hidden lair of AE Central. From cars and beer to hardcore software development, you might find interesting topics in all areas. Feel free to browse!
Home

To menu | To search

Delphi hexadecimal operations

, 09:41 - Permalink

Delphi 7I was recently working on a project where I had to work with huge numbers (>32 hexadecimal digits). Since not even the largest integer type was able to hold these, I had to come up with a solution to be able to do arithmetic functions on them.
Back to elementary school! Do you remember how we learned to add, divide, subtract or multiply on paper? I certainly didn't. After long research and coding, I managed to mimic it... in hexadecimal! The code is not optimized at all, but since the functions could work with 32 digit numbers under 10 ms I did not even bother. Feel free to opt them to your needs!
 
Function HexAdd(Hex1, Hex2: String): String;
Var
 h1, h2: String;
 c: Byte;
Begin
 Result := '';
 If (Hex1='') And (Hex2='') Then Exit;
 h1 := UpperCase(Hex1);
 h2 := UpperCase(Hex2);
 While Length(h1) < Length(h2) Do
  h1 := '0' + h1;
 While Length(h2) < Length(h1) Do
  h2 := '0' + h2;
 c := 0;
 While (h1<>'') And (h2<>'') Do
  Begin
   c := c + StrToInt('$'+h1[Length(h1)]) + StrToInt('$'+h2[Length(h2)]);
   If c > 15 Then Begin
                  Result := IntToHex(c-16,1) + Result;
                  c := 1;
                  End
     Else Begin
          Result := IntToHex(c,1) + Result;
          c := 0;
          End;
   Delete(h1,Length(h1),1);
   Delete(h2,Length(h2),1);
  End;
 If c > 0 Then Result := IntToHex(c,1) + Result;
End;
 
Function HexSubtract(Hex1, Hex2: String): String;
Var
 h1, h2: String;
 c: Byte;
Begin
 Result := '';
 If (Hex1='') And (Hex2='') Then Exit;
 h1 := UpperCase(Hex1);
 h2 := UpperCase(Hex2);
 While Length(h1) < Length(h2) Do
  h1 := '0' + h1;
 While Length(h2) < Length(h1) Do
  h2 := '0' + h2;
 c := 0;
 While (h1<>'') And (h2<>'') Do
  Begin
   If StrToInt('$'+h1[Length(h1)]) < StrToInt('$'+h2[Length(h2)]) Then Begin
                                                                       c := StrToInt('$1'+h1[Length(h1)]) - StrToInt('$'+h2[Length(h2)]) - c;
                                                                       Result := IntToHex(c,1) + Result;
                                                                       c := 1;
                                                                       End
     Else
   If h1[Length(h1)] = h2[Length(h2)] Then Begin
                                           If c = 0 Then Result := '0' + Result
                                             Else Result := 'F' + Result;
                                           End
     Else Begin
          // 7FFD - 3FFF = 3FFE !!!
          c := StrToInt('$'+h1[Length(h1)]) - StrToInt('$'+h2[Length(h2)]) - c;
          Result := IntToHex(c,1) + Result;
          c := 0;
          End;
   Delete(h1,Length(h1),1);
   Delete(h2,Length(h2),1);
  End;
 While (Result<>'0') And (Result[1]='0') Do
  Delete(Result,1,1);
 If c > 0 Then Result := '-' + Result;
End;
 
Function HexMultiply(Hex1, Hex2: String): String;
Var
 tempres: String;
 c: Byte;
 a1, a2: Integer;
Begin
 Result := '';
 If (Hex1='') Or (Hex2='') Then Exit;
 For a2 := Length(Hex2) DownTo 1 Do
  Begin
   tempres := '';
   c := 0;
   For a1 := 1 To Length(Hex2)-a2 Do
    tempres := '0' + tempres;
   For a1 := Length(Hex1) DownTo 1 Do
    Begin
     c := c + StrToInt('$'+Hex1[a1]) * StrToInt('$'+Hex2[a2]);
     tempres := IntToHex(c Mod 16,1) + tempres;
     c := c Div 16;
    End;
   If c > 0 Then tempres := IntToHex(c,1) + tempres;
   Result := HexAdd(Result,tempres);
  End;
End;
 
Function HexDivide(Hex1, Hex2: String): String;
Var
 a1, a2, c: Integer;
Begin
 Result := '';
 If (Hex1='') Or (Hex2='') Then Exit;
 If Length(Hex2) > Length(Hex1) Then Begin
                                     Result := '0';
                                     Exit;
                                     End;
 a1 := 1;
 c := 0;
 Repeat
  a2 := 1;
  While (a1+a2<Length(Hex1)) And (StrToInt('$'+IntToHex(c,1)+Copy(Hex1,a1,a2)) < StrToInt('$'+Hex2)) Do
   Inc(a2);
  Result := Result + IntToHex(StrToInt('$'+IntToHex(c,1)+Copy(Hex1,a1,a2)) Div StrToInt('$'+Hex2),1);
  c := StrToInt('$'+IntToHex(c,1)+Copy(Hex1,a1,a2)) Mod StrToInt('$'+Hex2);
  a1 := a1+a2;
 Until a1 > Length(Hex1);
End;
 

Add a comment

HTML code is displayed as text and web addresses are automatically converted.

This post's comments feed