{***********************************************************************}
{									}
{ __ToRadStr  Return the radix representation of an integer type	}
{									}
{ function  __ToRadStr(IntValue : longint;				}
{		       Size,Radix,Width : word) : string;		}
{									}
{	      IntValue	The integer-type expression to be converted to	}
{			a string of digits in the specified base.	}
{	      Size	The number of bytes occupied by IntValue.	}
{	      Width	The length of the returned string.		}
{	      Radix	The specified base.  It can be at most 36 and	}
{			must be at least 2.				}
{									}
{ Description __ToRadStr returns a string representing the value of	}
{	      IntValue in the base specified by the Radix parameter.	}
{	      For example, for Radix = 2, the binary representation	}
{	      is returned, and for 16, the hexadecimal representation	}
{	      is returned (see __ToHexStr).  The string is right justi- }
{	      fied in a field of Width length, left padded with zeros.	}
{	      If Width is less than the length required to represent	}
{	      IntValue, or greater than 255, the length of the returned }
{	      string is exactly the length required by the magnitude of }
{	      IntValue and the specified Radix, which will be:		}
{									}
{			 ln(Value) / ln(Radix) + 1.			}
{									}
{	      where Value is IntValue considered as unsigned.		}
{	      To specify Size, use the SizeOf function			}
{									}
{	      MyHexStr := __ToRadStr(MyVar,SizeOf(MyVar),16,0); 	}
{									}
{	      If an invalid base is specified, the NULL string is re-	}
{	      turned.  Please note that the unsigned bit representation }
{	      is always returned.  For example, the hexadecimal repre-	}
{	      sentation of the integer -1 is FFFF, of the shortint -1	}
{	      is FF, and of the longint -1 is FFFFFFFF. 		}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc.  1987		}
{_______________________________________________________________________}

function  __ToRadStr(IntValue : longint;
				Size,Radix,Width : word) : string;

  const
    MAX32BIT = 4294967296.0;	       { 2 * MAXLONGINT + 1		}

  const
    RadCheck : array[1..36] of char =
				  '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

  var
    Quotient		 : word;
    I, J, IVal		 : word;
    RVal, RQuotient	 : real;
    Remainder		 : real;
    TempStr, ReturnedStr : string;

  begin {__ToRadStr}

    if ((Radix < 2) or (Radix > 36) or
		    ((Size <> 1) and (Size <> 2) and (Size <> 4))) then
       begin
	 __ToRadStr := '';
	 Exit			{ Bad parameters, so exit immediately	}
       end;

    I := 0;
    case Size of
      1,2: begin		{ Bytes, shortints, words, integers	}
	    if (Size = 1) then
	      IVal := (IntValue shl 24) shr 24	 { Truncate to 1 byte	}
	    else
	      IVal := (IntValue shl 16) shr 16;  { Truncate to 2 bytes	}
	    if (IVal = 0) then
	      ReturnedStr := '0'
	    else
	      repeat
		Inc(I); 		       { Divide IVal by Radix,	}
		Quotient   := IVal div Radix;  { replace with Quotient. }
		TempStr[I] := RadCheck[(IVal mod Radix) + 1];
		IVal	   := Quotient;
	      until (IVal = 0);
	  end; { 1,2 }

     { For longints we assign L to a real value, since integer division }
     { for double words is not available.  We add the 2 ** 32		}
     { ($100000000) to convert negatives to unsigned representation.	}

      4 : if (IntValue = 0) then
	    ReturnedStr := '0'
	  else
	    begin
	      if (IntValue < 0) then
		RVal := IntValue + MAX32BIT
	      else
		RVal := IntValue;
	      repeat
		Inc(I);
		RQuotient := Trunc(RVal / Radix);
		Remainder :=
			 Trunc(RVal - Radix * 1.0 * trunc(RVal / Radix));
		TempStr[I] := RadCheck[round(Remainder) + 1];
		RVal	   := RQuotient;
	      until (RVal = 0);
	    end;

    end; {case Size}

    Length(ReturnedStr) := I;		{ Set the string length and	}
    for J := I downto 1 do		{ invert the string.		}
      ReturnedStr[J] := TempStr[I - J + 1];

    { Finally, right-justify the returned string and pad zeros on the	}
    { left, if necessary.						}

    if ((Width < length(ReturnedStr)) or (Width > MAXSTR_)) then
      __ToRadStr := ReturnedStr
    else
      __ToRadStr := __JustStr(ReturnedStr,'0',Width,_RIGHT_JUST_STR)

  end;	{__ToRadStr}
