Computer Science 101

with:  Erik Oosterwal

Search for specific programs or algorithms:
Custom Search





Alpha to Real


This routine will convert a string of alphanumeric characters (basically a string of digits) to a real number. This routine was required for a FORTRAN program I wrote in the late '80s that did not have that function built in.  In that program, I was interfacing with a serial data acquisition device that would respond to alpha numeric commands with some value in a predetermined format.  In this case, real numbers were returned in positions 5 through 9 of an array.



C     ******************************************************************
C     *    SUBROUTINE TO CONVERT ALPHA TO REAL                         *
C     *                                                                *
C     *    ARRY - THE STRING ARRAY WHICH CONTAINS THE ALPHA VALUE      *
C     *         OF THE NUMBER IN LOCATIONS ARRY(5) - ARRY(9)           *
C     *    L - THE LENGTH OF THE STRING ARRAY (THIS VALUE MUST BE      *
C     *         PASSED ALONG TO DIMENSION THE ARRAY)                   *
C     *    RNUM - THE VARIABLE THAT WILL CONTAIN THE NUMERIC VALUE OF  *
C     *         THE STRING.  THIS VARIABLE MUST BE DEFINED AS "REAL"   *
C     *         BECAUSE IT CAN CONTAIN FRACTIONS                       *
C     *    EXP - COUNTS NUMBER OF DECIMAL POSITIONS                    *
C     *    INC - SIGNIFIES WHEN TO START COUNTING FRACTIONAL DIGITS    *
C     *    INUM - INTEGER NUMBER THAT WHEN DIVIDED BY EXP IS EQUAL     *
C     *         TO RNUM                                                *
C     *    **** RNUM ALSO SIGNIFIES IF THE RETURNED VALUE IS POSITIVE  *
C     *         OR NEGATIVE                                            *
C     *                                                                *
C     *    WRITTEN BY:  ERIK P. OOSTERWAL                              *
C     *    STARTED ON:  NOV. 17, 1987                                  *
C     *    FINISHED  :  NOV. 19, 1987                                  *
C     ******************************************************************
      SUBROUTINE ATON(ARRY,L,RNUM)
C
      REAL RNUM
      INTEGER*2 L,INC,EXP,INUM
      CHARACTER*1 ARRY(L)
C
C     *    GET RID OF EXTRA CHARACTERS AT THE BEGINNING OF THE LINE    *
C     *    DOES THE SAME AS 'CALL ALIGN' *
    5 IF(ARRY(1).NE.'"')THEN
           DO 10, I = 1,(L-1)
   10           ARRY(I)=ARRY(I+1)
           GOTO 5
           ENDIF
C
C     *    FIND THE NUMERIC VALUE OF THE STRING    *
      INC = 0
      EXP = 0
      INUM = 0
      RNUM = 1
      DO 20, I = 5,9
C     *    IF THERE IS A '-', THEN THE NUMBER IS NEGATIVE    *
             IF(ARRY(I) .EQ. '-') THEN
                    RNUM = -1
                    GOTO 20
                    ENDIF
C     *    IF THERE IS A '.', KEEP TRACK OF THE NUMBER OF DECIMALS     *
             IF(ARRY(I) .EQ. '.') THEN
                    INC = 1
                    GOTO 20
                    ENDIF
             INUM = INUM*10+(ICHAR(ARRY(I))-48)
             EXP = EXP+INC
   20 CONTINUE
      RNUM = RNUM*(FLOAT(INUM)/10**EXP)
      RETURN
      END





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