Silverfrost Forums

Welcome to our forums

Converting integer vector to character

27 Feb 2014 4:50 #13768

Is there any FORTRAN intrinsic that converts integer vector to character?

Soemthing like

c on entry: c no_chars no. of characters c my_array integer array c c on return: c chars_variable character variable

27 Feb 2014 8:36 #13769

One way would be to use in internal WRITE statement...

CHARACTER(LEN=80)::str

WRITE(str, *) vector

You could add a format string to get the result in the way you want it

27 Feb 2014 9:08 #13771

Thanks, it worked.

I also need to convert character string to integer witht eh following but it didn't work. Any tips?

       Integer x
       Character(50)  xstring
       xstring = 'chris'
       Read( xstring, * )  x
27 Feb 2014 11:19 #13772

If the string to read is '42' it works perfectly well.

27 Feb 2014 8:44 #13773

Christy,

On the READs, use ERR= 'clauses' to catch errors. If you are reading INTEGER out of a character string, then the characters must be numerical digits followed by a valid separator, and you can test for that before you do a READ. You are much less likely to need END=

E

28 Feb 2014 7:42 #13774

With the following routine I read co-ordinate data from ASCII files. I read line-by-line as character string ('text'), then the numerical values from the string:

c
      SUBROUTINE READ_COORDS(unit,nr,x,y,z,rtcode)
c
c     reads (No., x, y, z) from file=unit
c
      IMPLICIT NONE
 
      integer*4      i,j,unit,nr,rtcode
      real*8         x,y,z
      character*200  text
 
      rtcode = 0
 
      read(unit,'(A200)',err=900,end=930)text
      read(text,*,err=100)nr,x,y,z
      return
 
c     replace non-numerical entries by ' '
 
100   do i = 199,1,-1
        j = ichar(text(i:i))
        if (j .lt. 45 .or. j .eq. 47 .or. j .gt. 57) text(i:i) = ' '
      end do

      read(text,*,err=900)nr,x,y,z
      return
 
900   rtcode = -1   ! failed 
      return
930   rtcode = 2    ! end-of-file reached
      return
      end

Regards - Wilfried

28 Feb 2014 8:20 #13775

I tried the following for converting integer to character amd vice versa. It works.Any comments?

 subroutine c2i(nchars,chars,iarray)
c
c  to convert character to integer vector
c
      character *(*) charsw
      dimension my_array(*)
c
      do 10 i=1,nchars
      my_array(i) = ichar(charsw(i:i))
   10 continue
c
      return
      end


   subroutine i2c(nchars,iarray,chars)
c
c  to convert character to integer vector
c
c on entry:
c     nchars     no. characters
c     chars      character variable
c
c on return:
c     iarray     integer vector
c
c##he
      character chars(80)
      character chars1(1)
      integer iarray(*)
c
      do 10 i=1,nchars
      chars1 = char(iarray(i:i))
      chars(i:i)=chars1(i)
   10 continue
c
      
      return
      end
28 Feb 2014 12:09 #13776

Christy, The code sample you provided does not compile and does not work.

I understand you want: c2i converts a character string to a set of ascii integer values for each character. i2c converts integer values to a string of theit ascii equivalent.

This is different to what Eddie and I understood you wanted, which was to convert a character string, say ' 2.34 ' to a numeric value 2.34 What you appear to want is to convert the character string ' 2.34 ' to an integer array (/ 32, 50, 46, 51, 52, 32 /)

What might do what you want is: subroutine c2i (nchars,chars,iarray) c c to convert character to integer vector c integer nchars character chars*() integer iarray() intrinsic ichar c do 10 i=1,nchars iarray(i) = ichar (chars(i:i)) 10 continue c return end

      subroutine i2c (nchars,iarray,chars) 
c 
c  to convert integer vector to character
c 
c on entry: 
c     nchars     no. characters 
c     iarray     integer vector 
c 
c on return: 
c     chars      character variable 
c 
      integer   nchars
      integer   iarray(*) 
      character chars*(*)
      intrinsic char
c 
      do 10 i=1,nchars 
         chars(i:i) = char (iarray(i)) 
   10 continue 
c 
      return 
      end

John

30 Jul 2014 8:35 #14379

I have to use character ascii values for determining the offset for centering proportional caharacters in a text with fixed distances.

I open the file with FORM='unformatted'. Then I load the file in a buffer as

INTEGER (kind=1), allocatable :: Input(:) !text must be of type ASCII < _128 read (9, IOSTAT=ios) Input; CLOSE (9)

Then I read the characters one by one in ascii values and read the affset from an array

ascii=Input(i) INTEGER offset(97:122) dx=offset(ascii)

I can read the characters from an array of characters or from the file in a character buffer

Menno

1 Aug 2014 3:18 #14381

What happens if the value of ascii is outside the range of 97:122 ? I presume there is more code that checks this. John

Please login to reply.