Conversion from int to char

This is a discussion on Conversion from int to char within the Fortran forums in Programming Languages category; 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?...

Go Back   Application Development Forum > Programming Languages > Fortran

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-29-2008, 10:28 AM
fm2766
Guest
 
Default Conversion from int to char

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?
Reply With Quote
  #2  
Old 08-29-2008, 10:56 AM
michaelmetcalf@compuserve.com
Guest
 
Default Re: Conversion from int to char

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
Reply With Quote
  #3  
Old 08-29-2008, 11:06 AM
Sebastian Gallinat
Guest
 
Default Re: Conversion from int to char

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.
Reply With Quote
  #4  
Old 08-29-2008, 11:18 AM
Paul van Delst
Guest
 
Default Re: Conversion from int to char

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
Reply With Quote
  #5  
Old 08-29-2008, 05:52 PM
e p chandler
Guest
 
Default Re: Conversion from int to char

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

Reply With Quote
  #6  
Old 08-29-2008, 10:05 PM
Ron Ford
Guest
 
Default Re: Conversion from int to char

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
Reply With Quote
  #7  
Old 08-29-2008, 10:35 PM
Gary Scott
Guest
 
Default Re: Conversion from int to char

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
Reply With Quote
  #8  
Old 08-30-2008, 12:36 AM
Richard Maine
Guest
 
Default Re: Conversion from int to char

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
Reply With Quote
  #9  
Old 08-30-2008, 12:43 AM
Richard Maine
Guest
 
Default Re: Conversion from int to char

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
Reply With Quote
  #10  
Old 08-30-2008, 01:37 AM
e p chandler
Guest
 
Default Re: Conversion from int to char



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]


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:29 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.