| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hello, I have to convert integers in characters; for example: from number i=123456 to string c="123456". I could write in a file the variable "i", and later read it as a variable "c", but this method seems to me very inefficient. Some suggestions? |
|
#2
| |||
| |||
| On Aug 29, 4:28*pm, fm2766 <fm2...@yahoo.it> wrote: > Hello, I have to convert integers in characters; for example: from > number i=123456 to string c="123456". > I could write in a file the variable "i", and later read it as a > variable "c", but this method seems to me very inefficient. > Some suggestions? Well, that's almost what you do, however, not to an external file but to an 'internal' one as defined by your c, as in: write(c, '(i6)') i HTH, Mike Metcalf |
|
#3
| |||
| |||
| fm2766 schrieb: > Hello, I have to convert integers in characters; for example: from > number i=123456 to string c="123456". > I could write in a file the variable "i", and later read it as a > variable "c", but this method seems to me very inefficient. > Some suggestions? First, declare a character variable which is long enough to hold your bigest integer number: character(len=15):: iBuf Second, use the write-statement performing an internal write to the character variable: write(unit=iBuf, fmt=*) i If needed, do something for adjusting the characters: iBuf = adjustl(iBuf) If you have some docs for the used compiler by hand, you will find this feature under internal write statement. Hope, this helps, Sebastian. |
|
#4
| |||
| |||
| michaelmetcalf@compuserve.com wrote: > On Aug 29, 4:28 pm, fm2766 <fm2...@yahoo.it> wrote: >> Hello, I have to convert integers in characters; for example: from >> number i=123456 to string c="123456". >> I could write in a file the variable "i", and later read it as a >> variable "c", but this method seems to me very inefficient. >> Some suggestions? > > Well, that's almost what you do, however, not to an external file but > to an 'internal' one as defined by your c, as in: > > write(c, '(i6)') i Maybe some thought should be given to giving this process an additional, more intuitive, name. It's such a common question and "Internal file writing and reading" sure isn't the first thing a newcomer thinks of when they want to convert int->character or vice versa. cheers, paulv |
|
#5
| |||
| |||
| Paul van Delst wrote: > michaelmetcalf@compuserve.com wrote: > > On Aug 29, 4:28 pm, fm2766 <fm2...@yahoo.it> wrote: > >> Hello, I have to convert integers in characters; for example: from > >> number i=123456 to string c="123456". > >> I could write in a file the variable "i", and later read it as a > >> variable "c", but this method seems to me very inefficient. > >> Some suggestions? > > > > Well, that's almost what you do, however, not to an external file but > > to an 'internal' one as defined by your c, as in: > > > > write(c, '(i6)') i > > Maybe some thought should be given to giving this process an additional, more intuitive, > name. It's such a common question and "Internal file writing and reading" sure isn't the > first thing a newcomer thinks of when they want to convert int->character or vice versa. Oh no. Let the C programmers learn some Fortran instead of whining about it. Learning a foreign language involves learning its idioms. To a FORTRAN programmer, the concept of "internal file" makes sense. - e |
|
#6
| |||
| |||
| On Fri, 29 Aug 2008 14:52:52 -0700 (PDT), e p chandler posted: > Paul van Delst wrote: >> michaelmetcalf@compuserve.com wrote: >>> On Aug 29, 4:28 pm, fm2766 <fm2...@yahoo.it> wrote: >>>> Hello, I have to convert integers in characters; for example: from >>>> number i=123456 to string c="123456". >>>> I could write in a file the variable "i", and later read it as a >>>> variable "c", but this method seems to me very inefficient. >>>> Some suggestions? >>> >>> Well, that's almost what you do, however, not to an external file but >>> to an 'internal' one as defined by your c, as in: >>> >>> write(c, '(i6)') i >> >> Maybe some thought should be given to giving this process an additional, more intuitive, >> name. It's such a common question and "Internal file writing and reading" sure isn't the >> first thing a newcomer thinks of when they want to convert int->character or vice versa. > > Oh no. Let the C programmers learn some Fortran instead of whining > about it. Learning a foreign language involves learning its idioms. To > a FORTRAN programmer, the concept of "internal file" makes sense. Gosh, elliot, I know that internal writes have been the answer for a lot of questions I've had since I renewed my commitment to fortran as a syntax. It's §9.6 in MR&C. What I don't see is a file. By "file," do people mean "whatever internal variables you used to read the data?" -- Unquestionably, there is progress. The average American now pays out twice as much in taxes as he formerly got in wages. 1 H. L. Mencken |
|
#7
| |||
| |||
| Ron Ford wrote: > On Fri, 29 Aug 2008 14:52:52 -0700 (PDT), e p chandler posted: > > >>Paul van Delst wrote: >> >>>michaelmetcalf@compuserve.com wrote: >>> >>>>On Aug 29, 4:28 pm, fm2766 <fm2...@yahoo.it> wrote: >>>> >>>>>Hello, I have to convert integers in characters; for example: from >>>>>number i=123456 to string c="123456". >>>>>I could write in a file the variable "i", and later read it as a >>>>>variable "c", but this method seems to me very inefficient. >>>>>Some suggestions? >>>> >>>>Well, that's almost what you do, however, not to an external file but >>>>to an 'internal' one as defined by your c, as in: >>>> >>>>write(c, '(i6)') i >>> >>>Maybe some thought should be given to giving this process an additional, more intuitive, >>>name. It's such a common question and "Internal file writing and reading" sure isn't the >>>first thing a newcomer thinks of when they want to convert int->character or vice versa. >> >>Oh no. Let the C programmers learn some Fortran instead of whining >>about it. Learning a foreign language involves learning its idioms. To >>a FORTRAN programmer, the concept of "internal file" makes sense. > > > Gosh, elliot, I know that internal writes have been the answer for a lot of > questions I've had since I renewed my commitment to fortran as a syntax. > It's §9.6 in MR&C. > > What I don't see is a file. By "file," do people mean "whatever internal > variables you used to read the data?" It's an abstract concept...the character variable is a substitute for a "file". -- Gary Scott mailto:garylscott@sbcglobal dot net Fortran Library: http://www.fortranlib.com Support the Original G95 Project: http://www.g95.org -OR- Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html If you want to do the impossible, don't hire an expert because he knows it can't be done. -- Henry Ford |
|
#8
| |||
| |||
| Gary Scott <garylscott@sbcglobal.net> wrote: > Ron Ford wrote: > > What I don't see is a file. By "file," do people mean "whatever internal > > variables you used to read the data?" > It's an abstract concept...the character variable is a substitute for a > "file". And that's really, really how you want to think of it. Because if you think of it that way, most other things about its usage pretty much follow (ok, not all of them, but most of the basics). For a very particular example, it is "obvious" when you want to do an internal read versus an internal write. If that isn't obvious, then I'd say you aren't yet really thinking about the character variable as a substitute for a file. If you don't think of it that way, then you'll be like all the other people who just try to memorize when to use read versus write, and who guess wrong pretty much 50% of the time. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain |
|
#9
| |||
| |||
| Richard Maine <nospam@see.signature> wrote: > Gary Scott <garylscott@sbcglobal.net> wrote: > > > Ron Ford wrote: > > > > What I don't see is a file. By "file," do people mean "whatever internal > > > variables you used to read the data?" > > It's an abstract concept...the character variable is a substitute for a > > "file". > > And that's really, really how you want to think of it. Because if you > think of it that way, most other things about its usage pretty much > follow (ok, not all of them, but most of the basics). For a very > particular example, it is "obvious" when you want to do an internal read > versus an internal write. If that isn't obvious, then I'd say you aren't > yet really thinking about the character variable as a substitute for a > file. If you don't think of it that way, then you'll be like all the > other people who just try to memorize when to use read versus write, and > who guess wrong pretty much 50% of the time. I know, following up to myself, but I realize I didn't adequately finish explaining the thought in answer to Paul. Thus just changing the terminology is not going to help anyone much in that while they might then be able to find the syntax, they won't be able to remember how to use it without looking it up every time. Much better, in my opinion, is to put index entries or whatever for the terms you think people are more likely to look up and say "see internal I/O". Yep, you need that kind of cross-referencing to help them find it. But once they do find it, you want them to think of it as internal I/O instead of as something like "smagic syntax for converting between character and numeric". -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain |
|
#10
| |||
| |||
| Ron Ford wrote: > On Fri, 29 Aug 2008 14:52:52 -0700 (PDT), e p chandler posted: > > > Paul van Delst wrote: > >> michaelmetcalf@compuserve.com wrote: > >>> On Aug 29, 4:28 pm, fm2766 <fm2...@yahoo.it> wrote: > >>>> Hello, I have to convert integers in characters; for example: from > >>>> number i=123456 to string c="123456". > >>>> I could write in a file the variable "i", and later read it as a > >>>> variable "c", but this method seems to me very inefficient. > >>>> Some suggestions? > >>> > >>> Well, that's almost what you do, however, not to an external file but > >>> to an 'internal' one as defined by your c, as in: > >>> > >>> write(c, '(i6)') i > >> > >> Maybe some thought should be given to giving this process an additional, more intuitive, > >> name. It's such a common question and "Internal file writing and reading" sure isn't the > >> first thing a newcomer thinks of when they want to convert int->character or vice versa. > > > > Oh no. Let the C programmers learn some Fortran instead of whining > > about it. Learning a foreign language involves learning its idioms. To > > a FORTRAN programmer, the concept of "internal file" makes sense. > > Gosh, elliot, I know that internal writes have been the answer for a lot of > questions I've had since I renewed my commitment to fortran as a syntax. > It's �9.6 in MR&C. > > What I don't see is a file. By "file," do people mean "whatever internal > variables you used to read the data?" Yes. I think of those variables as file buffers. Also using internal files opens up the opportunity to have dynamic format expressions. Since a format may be contained in a character variable, it is possible to write to that character variable in a creative way! - e [Aside] Back in ye olden days of interpreted Microsoft Basic, that language (v4 and v5) had "PRINT USING" for formatted file output. But it was not ordinarily possible to direct the output to a string, as is done with sprintf in C. IIRC there was some way to get Basic to open a file so that no external I/O actually took place. Instead it was possible to access memory where the file contents would be placed. [/ Aside] |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.