forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Converting integer vector to character

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Thu Feb 27, 2014 5:50 am    Post subject: Converting integer vector to character Reply with quote

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

Soemthing like

Quote:
c on entry:
c no_chars no. of characters
c my_array integer array
c
c on return:
c chars_variable character variable
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Thu Feb 27, 2014 9:36 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message AIM Address
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Thu Feb 27, 2014 10:08 am    Post subject: Reply with quote

Thanks, it worked.

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

Code:
       Integer x
       Character(50)  xstring
       xstring = 'chris'
       Read( xstring, * )  x
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Thu Feb 27, 2014 12:19 pm    Post subject: Reply with quote

If the string to read is '42' it works perfectly well.
Back to top
View user's profile Send private message AIM Address
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2388
Location: Yateley, Hants, UK

PostPosted: Thu Feb 27, 2014 9:44 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: Düsseldorf, Germany

PostPosted: Fri Feb 28, 2014 8:42 am    Post subject: Reply with quote

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:

Code:
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
Back to top
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Fri Feb 28, 2014 9:20 am    Post subject: Reply with quote

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

Code:
 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
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Fri Feb 28, 2014 1:09 pm    Post subject: Reply with quote

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:
Code:
      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
Back to top
View user's profile Send private message
Menno



Joined: 16 Jul 2014
Posts: 4

PostPosted: Wed Jul 30, 2014 9:35 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Fri Aug 01, 2014 4:18 am    Post subject: Reply with quote

What happens if the value of ascii is outside the range of 97:122 ?
I presume there is more code that checks this.
John
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group