| dp2hx |
|
Table of contents
Procedure
DP2HX ( D.p. number to hexadecimal string )
SUBROUTINE DP2HX ( NUMBER, HXSTR, HXSSIZ )
Abstract
Convert a double precision number to an equivalent character
string using a base 16 "scientific notation."
Required_Reading
None.
Keywords
ALPHANUMERIC
CONVERSION
Declarations
IMPLICIT NONE
INTEGER STRLEN
PARAMETER ( STRLEN = 255 )
DOUBLE PRECISION NUMBER
CHARACTER*(*) HXSTR
INTEGER HXSSIZ
Brief_I/O
VARIABLE I/O DESCRIPTION
-------- --- --------------------------------------------------
STRLEN P Max number of characters allowed in output string.
NUMBER I D.p. number to be converted.
HXSTR O Equivalent character string, left justified.
HXSSIZ O Length of the character string produced.
Detailed_Input
NUMBER is the double precision number to be converted to a
character string representation.
Detailed_Output
HXSTR is the character string produced by this routine that
represents NUMBER in a base 16 "scientific notation,"
e.g.:
672.0 = '2A^3' = ( 2/16 + 10/( 16**2 ) ) * 16**3
and
-11.0 = '-B^1' = - ( 11/16 ) * 16**1.
The following table describes the character set used to
represent the hexadecimal digits and their corresponding
values.
Character Value Character Value
--------- ------ --------- ------
'0' 0.0D0 '8' 8.0D0
'1' 1.0D0 '9' 9.0D0
'2' 2.0D0 'A' 10.0D0
'3' 3.0D0 'B' 11.0D0
'4' 4.0D0 'C' 12.0D0
'5' 5.0D0 'D' 13.0D0
'6' 6.0D0 'E' 14.0D0
'7' 7.0D0 'F' 15.0D0
The caret, or hat, character, '^', is used to
distinguish the exponent.
The plus sign, '+', and the minus sign, '-', are used,
and they have their usual meanings.
In order to obtain the entire character string produced
by this routine, the output character string should be
at least N characters long, where
# of bits per double precision mantissa + 3
N = 3 + ---------------------------------------------
4
# of bits per double precision exponent + 3
+ ---------------------------------------------
4
There should be one character position for the sign of
the mantissa, one for the sign of the exponent, one for
the exponentiation character, and one for each
hexadecimal digit that could be produced from a mantissa
and an exponent.
The following table contains minimum output string
lengths necessary to obtain the complete character
string produced by this routine for some typical
implementations of double precision numbers.
Double precision number
Size Mantissa Exponent Minimum output string
bits bits bits length
---- -------- -------- ----------------------
64 48 15 3 + 12 + 4 = 19
64 55+1 8 3 + 14 + 2 = 19 (VAX)
64 52 11 3 + 13 + 3 = 19 (IEEE)
The base 16 "scientific notation" character string
produced by this routine will be left justified and
consist of a contiguous sequence of characters with one
of the following formats:
(1) h h h h ... h ^H H ... H
1 2 3 4 n 1 2 m
(2) -h h h h ... h ^H H ... H
1 2 3 4 n 1 2 m
(3) h h h h ... h ^-H H ... H
1 2 3 4 n 1 2 m
(4) -h h h h ... h ^-H H ... H
1 2 3 4 n 1 2 m
where
h and H denote hexadecimal digits
i j
'^' denotes exponentiation ( base 16 )
and
'+' and '-' have their usual interpretations.
The character string produced will be blank padded on
the right if HXSSIZ < LEN( HXSTR ).
HXSSIZ is the length of the base 16 "scientific notation"
character string produced by this routine.
Parameters
STRLEN is the maximum number of characters permitted in the
output string. The value of STRLEN is 255.
Exceptions
Error free.
1) If the output character string is not long enough to
contain the entire character string that was produced,
the string will be truncated on the right.
2) If LEN( HXSTR ) > HXSSIZ, the output character string will
be blank padded on the right.
Files
None.
Particulars
This routine converts a double precision number into an equivalent
character string using a base 16 "scientific notation." This
representation allows the full precision of a number to be placed
in a format that is suitable for porting or archival storage.
This routine is one of a pair of routines which are used to
perform conversions between double precision numbers and
an equivalent base 16 "scientific notation" character string
representation:
DP2HX -- Convert a double precision number into a base 16
"scientific notation" character string.
HX2DP -- Convert a base 16 "scientific notation"
character string into a double precision number.
Examples
The numerical results shown for this example may differ across
platforms. The results depend on the SPICE kernels used as
input, the compiler and supporting libraries, and the machine
specific arithmetic implementation.
1) Convert a set of double precision numbers to their equivalent
character string using a base 16 "scientific notation."
Example code begins here.
PROGRAM DP2HX_EX1
IMPLICIT NONE
C
C Local constants.
C
INTEGER HXSLEN
PARAMETER ( HXSLEN = 40 )
C
C Local variables.
C
CHARACTER*(HXSLEN) STRVAL
DOUBLE PRECISION NUMBER (8)
INTEGER I
INTEGER SVALLN
C
C Assign an array of double precision numbers.
C
DATA NUMBER / 2.0D-9, 1.0D0,
. -1.0D0, 1024.0D0,
. -1024.0D0, 521707.0D0,
. 27.0D0, 0.0D0 /
C
C Loop over the NUMBER array, call DP2HX for each
C element of NUMBER.
C
WRITE(*,*) 'number string length'
WRITE(*,*) '----------- ----------------- ------'
DO I= 1, 8
CALL DP2HX ( NUMBER(I), STRVAL, SVALLN )
WRITE(*,'(E12.4,2X,A17,2X,I5)') NUMBER(I), STRVAL,
. SVALLN
END DO
END
When this program was executed on a Mac/Intel/gfortran/64-bit
platform, the output was:
number string length
----------- ----------------- ------
0.2000E-08 89705F4136B4A8^-7 17
0.1000E+01 1^1 3
-0.1000E+01 -1^1 4
0.1024E+04 4^3 3
-0.1024E+04 -4^3 4
0.5217E+06 7F5EB^5 7
0.2700E+02 1B^2 4
0.0000E+00 0^0 3
Note: the hat or caret, '^', signals an exponent.
Restrictions
1) The maximum number of characters permitted in the output
string is specified by the parameter STRLEN.
Literature_References
None.
Author_and_Institution
J. Diaz del Rio (ODC Space)
K.R. Gehringer (JPL)
Version
SPICELIB Version 1.1.0, 06-JUL-2021 (JDR)
Changed the argument names "STRING" and "LENGTH" to "HXSTR"
and "HXSSIZ" for consistency with other routines.
Added IMPLICIT NONE statement.
The declaration of STRLEN has been promoted to the
$Declarations section.
Edited the header to comply with NAIF standard. Removed
unnecessary $Revisions section.
Added complete example code.
Updated $Brief_I/O, $Parameters and $Restrictions sections to
properly describe STRLEN.
SPICELIB Version 1.0.1, 10-MAR-1994 (KRG)
Fixed a typo in the description of the input argument STRING.
The example showing the expansion of 160 into hexadecimal
was incorrect. 160 was replaced with 672 which makes the
example correct.
SPICELIB Version 1.0.0, 26-OCT-1992 (KRG)
|
Fri Dec 31 18:36:14 2021