How to Convert Decimal Numbers to Hexadecimal Numbers

ID: Q95717

2.50 2.50a | 2.00 2.50 2.50a

WINDOWS    | MS-DOS
kbprg

The information in this article applies to:

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
Keywords          : FxprgGeneral 
Version           : 2.50 2.50a | 2.00 2.50 2.50a
Platform          : MS-DOS WINDOWS

Last Reviewed: August 31, 1996