|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Thu Feb 27, 2014 5:50 am Post subject: Converting integer vector to character |
|
|
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 |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7916 Location: Salford, UK
|
Posted: Thu Feb 27, 2014 9:36 am Post subject: |
|
|
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 |
|
|
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Thu Feb 27, 2014 10:08 am Post subject: |
|
|
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 |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7916 Location: Salford, UK
|
Posted: Thu Feb 27, 2014 12:19 pm Post subject: |
|
|
If the string to read is '42' it works perfectly well. |
|
Back to top |
|
|
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2388 Location: Yateley, Hants, UK
|
Posted: Thu Feb 27, 2014 9:44 pm Post subject: |
|
|
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 |
|
|
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: Düsseldorf, Germany
|
Posted: Fri Feb 28, 2014 8:42 am Post subject: |
|
|
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 |
|
|
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Fri Feb 28, 2014 9:20 am Post subject: |
|
|
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 |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Fri Feb 28, 2014 1:09 pm Post subject: |
|
|
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 |
|
|
Menno
Joined: 16 Jul 2014 Posts: 4
|
Posted: Wed Jul 30, 2014 9:35 am Post subject: |
|
|
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 |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Fri Aug 01, 2014 4:18 am Post subject: |
|
|
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 |
|
|
|
|
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
|