在Fortran 90中将字符串转换为整数 [英] Converting a string to an integer in Fortran 90
本文介绍了在Fortran 90中将字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道 IACHAR(s)
在字符串s的第一个字符位置返回ASCII字符的代码,但我需要将整个字符串转换为整数。我也有一些字符串(大约30个字符串,每个字符串最多包含20个字符)。有没有办法将它们中的每一个转换为Fortran 90中的唯一整数?
I know that IACHAR(s)
returns the code for the ASCII character in the first character position of the string s, but I need to convert the entire string to an integer. I also have a few number of strings (around 30 strings, each consists of at most 20 characters). Is there any way to convert each one of them to a unique integer in Fortran 90?
推荐答案
您可以将
字符串读入整数变量:
You can read
a string into an integer variable:
module str2int_mod
contains
elemental subroutine str2int(str,int,stat)
implicit none
! Arguments
character(len=*),intent(in) :: str
integer,intent(out) :: int
integer,intent(out) :: stat
read(str,*,iostat=stat) int
end subroutine str2int
end module
program test
use str2int_mod
character(len=20) :: str(3)
integer :: int(3), stat(3)
str(1) = '123' ! Valid integer
str(2) = '-1' ! Also valid
str(3) = 'one' ! invalid
call str2int(str,int,stat)
do i=1,3
if ( stat(i) == 0 ) then
print *,i,int(i)
else
print *,'Conversion of string ',i,' failed!'
endif
enddo
end program
这篇关于在Fortran 90中将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文