Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!clarkson!sun.soe.clarkson.edu!taylor Date: Tue, 2 Apr 91 08:07:21 EST From: Ross Taylor Message-Id: <9104021307.AA25164@sun.soe.clarkson.edu> Subject: Re: Checking a string for an integer Newsgroups: comp.lang.fortran Here's my contribution to this discussion: two routines for converting strings of any length to integers or real numbers. there are no read statements and as far as I know the code is completely standard F77. Ross Taylor Department of Chemical Engineering Clarkson University Potsdam, NY 13699 (yes, we have our own zip code) taylor@sun.soe.clarkson.edu C ******************************************************************* C * * C * SUBROUTINE CHRTOI * C * * C ******************************************************************* C SUBROUTINE CHRTOI (INT, STRING, IERR) C C Function C -------- C C To return an integer stored as a character string C C Arguments C --------- C C INT - The integer in numeric form (output) C STRING - The character string (input) C IERR - Error flag set to 1 if non-numeric character found C C Remarks C ------- C C Code written by ross taylor, delft, march 7, 1988 C Inspired by code written by malcolm woodman C CHARACTER*(*) STRING C C Initialize integer C INT = 0 IERR = 0 C C Determine length of character string C LENGTH = LEN(STRING) C C Check for sign in position 1 C L1 = 1 IF (STRING(1:1) .EQ. '-' .OR. STRING(1:1) .EQ. '+') L1 = 2 L2 = LENGTH + L1 C DO 1 I = L1, LENGTH C K = L2 - I C C Starting from the last character C Convert to integer (ichar returns ascii code) (0 = 48; 9 = 57) C KK = ICHAR (STRING(K:K)) - 48 C C Check for non-numeric characters C IF (KK .LT. 0 .OR. KK .GT. 9) THEN INT = 0 IERR = 1 RETURN ENDIF C C Add current contribution to int C INT = INT + KK * 10 ** (I-L1) C 1 CONTINUE C IF (STRING(1:1) .EQ. '-') INT = - INT C RETURN END C C ******************************************************************* C * * C * subroutine chrtor * C * * C ******************************************************************* C SUBROUTINE CHRTOR (REAL, STRING, IERR) C C Function C -------- C C To return a real number stored as a character string C C Arguments C --------- C C REAL - The number in numeric form (output) C STRING - The character string (input) C IERR - Error flag set to 1 if non-numeric character found C C Remarks C ------- C C This subroutine works by breaking up the input character string C Into three segments: the integer part, the decimal part and the C Exponent. subroutine chrtoi is used to convert all three C Character strings to intgers and the results combined to give the C Real number output C C This code written by ross taylor, delft, march 8, 1988 C Inspired by code written by malcolm woodman C C Notation C -------- C C INT - The value of the integer contribution C IEXP - The exponent C IDEC - The decimal contribution C IERR - Error flag set to unity if non-numeric character found C ISTART - Location in string of first character of current part C C The following character strings all give the same result C C 10 10. 10.0 10.d0 10d0 1d1 = 10.0 C C IMPLICIT DOUBLE PRECISION (A-H, O-Z) C CHARACTER*(*) STRING C C Initialize various items C INT = 0 IDEC = 0 IEXP = 0 IERR = 0 ISTART = 1 REAL = 0.D0 C C Determine length of input character string C LENGTH = LEN(STRING) C C Look for decimal point to mark end of integer part C IPOINT = INDEX(STRING,'.') C C Look for 'd' or 'e' to mark exponent C IPEXP = INDEX(STRING,'D') C IF (IPEXP .EQ. 0) IPEXP = INDEX(STRING,'E') IF (IPEXP .EQ. 0) IPEXP = INDEX(STRING,'d') IF (IPEXP .EQ. 0) IPEXP = INDEX(STRING,'e') C C Convert character string to integer C IF (IPOINT .NE. 0) THEN CALL CHRTOI (INT, STRING(ISTART:IPOINT-1), IERR) ELSE IF (IPEXP .NE. 0) THEN CALL CHRTOI (INT, STRING(ISTART:IPEXP-1), IERR) ELSE CALL CHRTOI (INT, STRING(ISTART:LENGTH), IERR) ENDIF C IF (IERR .NE. 0) THEN REAL = 0.D0 RETURN ENDIF C REAL = DBLE(INT) C C Next, the decimal part of the result C IF (IPOINT .NE. 0) THEN C ISTART = IPOINT + 1 C C Determine length of decimal part C IF (IPEXP .GT. IPOINT) THEN LENDEC = IPEXP - IPOINT ELSE LENDEC = LENGTH - IPOINT ENDIF C C Convert character string to integer C IF (LENDEC .GT. 0) THEN CALL CHRTOI (IDEC,STRING(ISTART:ISTART+LENDEC-1),IERR) ENDIF C IF (IERR .NE. 0) THEN DEC = 0.D0 ENDIF C DEC = DBLE(IDEC) / 10.0 ** LENDEC C IF (STRING(1:1) .EQ. '-') THEN REAL = REAL - DEC ELSE REAL = REAL + DEC ENDIF C ENDIF C C Finally, the exponent (if present) C IF (IPEXP .GT. 0) THEN C ISTART = IPEXP + 1 C C Convert character string to integer C CALL CHRTOI (IEXP,STRING(ISTART:LENGTH),IERR) C IF (IERR .NE. 0) THEN IEXP = 0 RETURN ENDIF C REAL = REAL * 10.D0 ** IEXP C ENDIF C RETURN END