For Programmers: Free Programming Magazines  


Home > Archive > Fortran > December 2006 > Converting string to Integer, Real, etc...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Converting string to Integer, Real, etc...
Jeremy

2006-12-27, 7:04 pm

How can I convert a string to an integer and other types? What
about the reverse? I do not see an intrinsic for it.

Jeremy
Dick Hendrickson

2006-12-27, 7:04 pm

Jeremy wrote:
> How can I convert a string to an integer and other types? What
> about the reverse? I do not see an intrinsic for it.
>
> Jeremy

It's called an internal read or internal write. in effect, you
use a character variable as the "file name" and read or write
the I/O list. Any book on Fortran should cover it. You do
things like

read(character_variable, format) list of variables

to "convert a string to an integer".

You want to be a little careful with a "string". Technically,
Fortran doesn't have them. It has character variables and they
are a little different from C strings. In particular, they have a
fixed length and don't have any special termination character.
Blank fill happens automagically in places where you might not
expect it.

Dick Hendrickson
dpb

2006-12-27, 7:04 pm


Jeremy wrote:
> How can I convert a string to an integer and other types? What
> about the reverse? I do not see an intrinsic for it.


No, in Fortran use internal READ (and WRITE for other way).

You use the same features of external i/o except from/to an internal
file (in it's most straight-forward case a character variable or array)
instead of an external file.

Beliavsky

2006-12-27, 7:04 pm


Dick Hendrickson wrote:
> Jeremy wrote:
> It's called an internal read or internal write. in effect, you
> use a character variable as the "file name" and read or write
> the I/O list. Any book on Fortran should cover it. You do
> things like
>
> read(character_variable, format) list of variables
>
> to "convert a string to an integer".
>
> You want to be a little careful with a "string". Technically,
> Fortran doesn't have them. It has character variables and they
> are a little different from C strings. In particular, they have a
> fixed length and don't have any special termination character.
> Blank fill happens automagically in places where you might not
> expect it.


I just started a "Fortran FAQ" Wikibook at
http://en.wikibooks.org/wiki/Fortran_FAQ . Anyone can contribute. Would
you mind if I revised your answer slightly and posted it there?

dpb

2006-12-27, 7:04 pm


Dick Hendrickson wrote:
....
> You want to be a little careful with a "string". Technically,
> Fortran doesn't have them. It has character variables and they
> are a little different from C strings. In particular, they have a
> fixed length and don't have any special termination character.
> Blank fill happens automagically in places where you might not
> expect it.


And, simply to fill out the other end of the spectrum :), truncation if
the target character variable isn't long enough to hold the operand.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com