How to Convert Decimal Numbers to Hexadecimal Numbers

Last reviewed: August 30, 1996
Article ID: Q95717
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5 and 2.5a
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, and 2.5a

The sample program below (TEST.PRG) demonstrates how to use the CONVERT() function in a program. The CONVERT() function converts a decimal number to a hexadecimal number. The function accepts one parameter: the decimal number to be converted to the hexadecimal number.

   * PROGRAM TEST.PRG

   SET TALK OFF
   CLEAR

   Dec_Number=0
   @ 3,5 SAY ;
   "Program to Convert a Decimal Number into a Hex Number"
   @ 5,5 SAY ;
   "Enter a Decimal Number " GET Dec_Number PICTURE '@k!!'

   READ

   * CONVERT() function being called
   Hex_Number=CONVERT(Dec_Number)
   @ 7,5 SAY "The Equivalent Hex_Number is " + Hex_Number

   * Start of FUNCTION CONVERT
   FUNCTION CONVERT
   * Parameter to be passed
   PARAMETER Dec_Number
   PRIVATE Answer, Val1,Val2,Val3,Val4, Num1,Num2,Num3

   * Use the INT() function to find the
   * integer portion of the value.
   Val1 = INT(Dec_Number / 4096)
   Num1 = Val1 * 4096
   Val2 = INT((Dec_Number - Num1) / 256)
   Num2 = Val2 * 256
   Val3 = INT((Dec_Number - Num1 - Num2) / 16)
   Num3 = Val3 * 16
   Val4 = INT(Dec_Number - Num1 - Num2 - Num3)
   ANSWER = COV(Val1) + COV(Val2) + COV(Val3) + COV(Val4)
   RETURN(ANSWER)

   * Use FUNCTION COV to pass a parameter and return
   * with the correct character expression associated with
   * the parameter.

   FUNCTION COV
   PARAMETER Num
   * Use the CHR() function to return with the appropriate
   * character.
   IF num > 9
     One = CHR(num + 55)
     RETURN(One)
   ELSE
     One = CHR(num + 48)
     RETURN(One)
   ENDIF


Additional reference words: FoxDos FoxWin 2.50 2.00 2.50a
KBCategory: kbprg
KBSubcategory: FxprgGeneral


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 30, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.