Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Enhancing the IntToHex

Title: Enhancing the IntToHex Question: IntToHex requires two arguments: 1. the integer to be transformed; 2. the length of output response. For the negative values that is the best solution you can have, but for the positive, you don't really need to specific an output length. So, you can simply use that function and that function will be granted. Answer: That's the solution. // If you won't use negative numbers, then no NegativeLength is required. // If you don't specify a NegativeLength for negative values it will use // a 32 bit standard hexadecimal notation. Function EnhIntToHex( Value : Integer; NegativeLength : Integer = 8 ) : String; Var IdX : Integer; Begin If ( NegativeLength 8 ) Then NegativeLength := 8; If ( NegativeLength If ( Value // If it's a negative, it returns the complete number with specified // maximum length. Result := IntToHex( Value, NegativeLength ); Exit; End Else Begin If ( Value = 0 ) Then Begin // Avoid problem of removing the only "0" that's needed. Result := '0'; Exit; End Else Begin // Converts to a great hexadecimal (8 is the max necessary...) Result := IntToHex( Value, 10 ); End; End; // Removes the initial zeros. If ( Length( Result ) 0 ) Then Begin For IdX := 1 To Length( Result ) Do If ( Result[ IdX ] '0' ) Then Break; If ( IdX 1 ) Then Begin Result := Copy( Result, IdX, MaxInt ); End; End; End;