Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!image.soe.clarkson.edu!taylor From: taylor@sun.soe.clarkson.edu (Ross Taylor) Newsgroups: comp.lang.fortran Subject: Char to real Message-ID: <1990Aug24.133337.4114@sun.soe.clarkson.edu> Date: 24 Aug 90 13:33:37 GMT Reply-To: taylor@sun.soe.clarkson.edu (Ross Taylor) Organization: Clarkson University, Potsdam, NY Lines: 90 I am looking for help with a problem converting character strings to real numbers. Here is the background to the problem. I have a binary data file created by a Turbo Pascal program. The data records are roughly 700 bytes each made up of a series of 4 byte reals and 4 byte integers. I need to read a data record with a FORTRAN program (the file is unformatted, direct access) as a char string of full length (approx 700 bytes), then process the string 4 bytes at a time. Each 4 byte substring is either a special sequence of bytes (ASCII 255) indicating that some particular calculation is to be performed OR string contains the IEEE representation of an integer or real number and must be converted to one of these two formats (I always know which). My problem is that I cannot figure out a way of doing the data conversion THAT IS ABSOLUTELY STANDARD FORTRAN 77 AND COMPLETELY PORTABLE and does not involve any physical disk access. The code will be running on a variety of machines (PC, VAX, IBM, Sun) under a variety of OS's (DOS, VMS, CMS, unix) so portability is extremely important. I tried using an internal file for the data conversion as shown below: FUNCTION TOREAL (STRING) C C Function C -------- C C To convert a character string to a real number C C Input C ----- C C STRING - Character string C C STRING should be four bytes in length. C C If STRING is not four bytes in length, the string C is replaced with a 4 byte character string, each byte C being ASCII chararacter number 255. C C Output C ------ C C TOREAL - The real number C CHARACTER STRING*(*), STR4*4, FILE*4 C C Check string is four bytes C IF (LEN(STRING) .NE. 4) THEN WRITE (*,*) 'Function TOREAL called with invalid argument' C C DO SOMETHING HERE C ELSE C STR4(1:4) = STRING(1:4) C C Read from internal file as a real number C READ (UNIT=STR4(1:4), FMT='(A)') X C C Assign X to the function value C TOREAL = X C ENDIF C RETURN END This code compiles and executes perfectly with WATFOR77 on a PC. It also compiles without error using Microsoft Fortran 4.01 and FTN77/386. However, the last two give errors during execution because there is an ANSI violation in the READ statement (incompatible format and number). I feel sure that the char to real data conversion problem has been addressed many times by others. Can someone please point me in the right direction. Many thanks in advance. Ross Taylor Department of Chemical Engineering Clarkson University, Potsdam, NY 13699 email: taylor@sun.soe.clarkson.edu