将字符串转换为整数 [英] Converting character string to integer

查看:188
本文介绍了将字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 get_command_argument()问题

我在阅读命令行参数( arg )转换成Fortran程序。然后,我想将 arg 的值存储为一个整数。 ichar()不会完成这项工作。

I'm reading a command line argument (arg) into a Fortran program. Then I want to store the value of arg as an integer. ichar() doesn't do the job.

这看起来很基本,很清楚我在做有问题。任何提示?

This seems kind of basic, so clearly I'm doing something wrong. Any hints?

program test_get_command_argument
   integer :: i,j
   character(len=32) :: arg

   i = 0
   do
       call get_command_argument(i,arg)
       if (LEN_TRIM(arg) == 0) EXIT

       write (*,*) trim(arg)
       i = i + 1
   end do

   j = ichar(arg)


end program


推荐答案

您想使用内部文件能力。
您应该有一个如 read(arg,*)j 的语句。
这将读取字符变量 arg ,就好像它是一个文件
并将结果存储到 j

You want to use the "internal files" capability. You should have a statement like read(arg,*) j. This will read the character variable arg as if it were a file and store the result into j.

这篇关于将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆