with: Erik Oosterwal
Decimal to Hexadecimal
This routine wil convert an integer to hexadecimal. It was originally
written for a FORTRAN project I was workign on back in the late '80s that
did not have that function built into the compiler. The routine is
pretty straight forward but is limited to Hex numbers of only 4 bytes. It
would be easy enough to allow larger hex strings by increasing the size of
the string array (ARRY(4)) to 8, 16, or whatever size you need. In
order for the rountine to work properly, your language/compiler must truncate
all non-whole number parts from integers when division results in a non-integer
number. MOD() returns the modulus, or remainder, of a division
operation.
C ******************************************
C
C ROUTINE TO CONVERT INTEGER VALUES TO HEX
C
C WRITTEN BY: ERIK PETER OOSTERWAL
C WRITTEN: SEP 12, 1988
C
C ******************************************
SUBROUTINE DTOH(INUM,ARRY)
C
CHARACTER*1 ARRY(4),HEX(16)
INTEGER*2 INUM
DATA HEX/'0','1','2','3','4','5','6','7','8',
* '9','A','B','C','D','E','F'/
C
DO 10 I = 4,1,-1
ARRY(I)=HEX(MOD(INUM,16)+1)
10 INUM = INUM/16
C
RETURN
END
For a more generalized routine that can convert between any two numeric bases,
see the Numeric Base Conversion article from
the main Computer Science 101 page.
Discuss computer algorithms and other computer science topics at the Computer Algorithms blog page.
All code and original algorithms are © Erik Oosterwal - 1987-2008
Computer Science 101
Dressing for Success