home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / gtmath05.prg < prev    next >
Text File  |  1993-10-14  |  3KB  |  86 lines

  1. /*
  2.  * File......: GTMATH05
  3.  * Author....: Philip Ide
  4.  * BBS.......: Dark Knight Returns
  5.  * Net/Node..:
  6.  * User Name.: Philip Ide
  7.  * Date......: $Date$
  8.  * Revision..: $Revision$
  9.  * Log file..: $Logfile$
  10.  *
  11.  * This is an original work by Philip Ide and is placed in the
  12.  * public domain.
  13.  *
  14.  * Modification history:
  15.  * ---------------------
  16.  *
  17.  * $Log$
  18.  *
  19.  */
  20.  
  21. /*  $DOC$
  22.  *  $FUNCNAME$
  23.  *      GT_DTOH()
  24.  *  $CATEGORY$
  25.  *      Maths
  26.  *  $ONELINER$
  27.  *      Turn a decimal number into hex
  28.  *  $SYNTAX$
  29.  *      GT_DtoH(<n> [,<nLength>]) --> <cHex>
  30.  *  $ARGUMENTS$
  31.  *      <n>       is a decimal integer to convert
  32.  *
  33.  *      <nLength> is the ^bMINIMUM^b length string to return.  If the
  34.  *                return string <cHex> is already longer than this, it
  35.  *                is not truncated.  If shorter, it is padded with leading
  36.  *                zero's.
  37.  *  $RETURNS$
  38.  *      <cHex>    is a string representing a hexadecimal number.
  39.  *  $DESCRIPTION$
  40.  *      This function allows you to convert from decimal to hex easily.  There
  41.  *      are many uses for hex numbres in Clipper.
  42.  *  $EXAMPLES$
  43.  *      ? GT_DtoH(255)         // 'FF'
  44.  *
  45.  *      // This code builds attribute bytes...
  46.  *      //
  47.  *      // Attribute color table:
  48.  *      // 0  = Black       8 = Grey
  49.  *      // 1  = Blue        9 = Bright Blue
  50.  *      // 2  = Green      10 = Bright Green
  51.  *      // 3  = Cyan       11 = Bright Cyan
  52.  *      // 4  = Red        12 = Bright Red
  53.  *      // 5  = Magenta    13 = Bright Magenta
  54.  *      // 6  = Brown      14 = Yellow
  55.  *      // 7  = White      15 = Bright White
  56.  *
  57.  *      cAttribute := SubStr(SaveScreen(0,0,0,0),2,1) // get att. byte
  58.  *      cAttribute := GT_DtoH(Asc(cAttribute))        // make 2bytes
  59.  *
  60.  *      /* now change background byte to bright/blinking white*/
  61.  *      cAttribute := GT_DtoH(15)+SubStr(cAttribute,-1)
  62.  *  $SEEALSO$
  63.  *      GT_HTOD()
  64.  *  $INCLUDE$
  65.  *      gt_lib.ch
  66.  *  $END$
  67.  */
  68.  
  69. #xcommand DEFAULT <a> TO <y> [,<b> TO <z>] ;
  70.      => <a> := IF(<a> == NIL, <y>, <a>) ;
  71.      [; <b> := IF(<b> == NIL, <z>, <b>)]
  72.  
  73. STATIC hex := '0123456789ABCDEF'
  74.  
  75. FUNCTION GT_DtoH(n,l) // n=Decnum, l=length of hex string to return
  76. LOCAL retval := "", x, y
  77.  
  78. DEFAULT l TO 4
  79. While (n>0)                                 // while we got a number...
  80.    x := n%16                                // get modulus
  81.    n := Int(n/16)                           // remove an exponent
  82.    retval := SubStr(hex, x+1, 1)+retval     // add result to return string
  83. End
  84. l := Max(l,Len(retval))
  85. Return (SubStr(Replicate('0',l)+retval,-l)) // ret padded with leading zero's
  86.