How to convert integers to characters? - Fortran

This is a discussion on How to convert integers to characters? - Fortran ; Is there any functions to convert integers to characters? For example: real::a=123.4 ! a is a real number character(len=30)::b ! b is a character !How can I convert a to b as a character, i.e. b would be 123.4, but ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

How to convert integers to characters?

  1. Default How to convert integers to characters?

    Is there any functions to convert integers to characters?

    For example:
    real::a=123.4 ! a is a real number
    character(len=30)::b ! b is a character
    !How can I convert a to b as a character, i.e. b would be 123.4, but b
    is a character.

    Moreover, is there any other functions can reverse the conversion?
    i.e. from characters to numbers.

    character(len=30)::b="123.4"
    real::a

    how can I assign 123.4 to a as a real number?

    Many thanks!


  2. Default Re: How to convert integers to characters?

    In message 29 Sep
    hermitian <iamwuxin@gmail.com> wrote:

    > Is there any functions to convert integers to characters?

    [snip]
    >
    > Moreover, is there any other functions can reverse the conversion?

    [snip]

    Have a look at these two functions. You should be able to do the reverse.

    * atodbl: converts string to real*8 using internal file

    function atodbl(str)

    implicit none

    c paramater declarations
    include "misc.h"

    c global declarations
    character str*(*)
    real*8 atodbl

    c external declarations
    integer*2 strlen

    c local declarations
    character cnvstr*(MAXSTRLEN),tmpstr*(MAXSTRLEN)
    logical*1 nodp
    integer*2 i,l
    real*8 tmp

    c begin

    tmpstr = str

    c.....check for decimal points
    l = strlen(str)
    i = 1
    nodp = .true.
    do while (i.le.l)
    nodp = nodp.and.(str(i:i).ne.'.')
    i = i + 1
    enddo

    if (nodp) then
    write(cnvstr,CHRFMT) tmpstr(1:l)//'.'
    else
    write(cnvstr,CHRFMT) tmpstr
    endif
    read(cnvstr,DBLFMT) tmp
    atodbl = tmp

    return
    end

    * atoint: converts string to integer*2 using internal file

    function atoint(str)

    implicit none

    c parameter declarations
    include "misc.h"

    c global declarations
    character str*(*)
    integer*2 atoint

    c local declarations
    character cnvstr*(MAXSTRLEN)
    integer*2 tmp

    c begin

    write(cnvstr,CHRFMT) str
    read(cnvstr,INTFMT) tmp
    atoint = tmp


    return
    end

    Hope this helps.

    Cheers,
    --
    Regards,
    Terry Duell


  3. Default Re: How to convert integers to characters?

    Thank you very much!

    But, is there any pre-defined internal functions could be used
    directly?



  4. Default Re: How to convert integers to characters?

    "hermitian" <iamwuxin@gmail.com> wrote in message
    news:1191025165.820407.47400@y42g2000hsy.googlegroups.com...
    > Is there any functions to convert integers to characters?
    >
    > For example:
    > real::a=123.4 ! a is a real number
    > character(len=30)::b ! b is a character
    > !How can I convert a to b as a character, i.e. b would be 123.4, but b
    > is a character.
    >
    > Moreover, is there any other functions can reverse the conversion?
    > i.e. from characters to numbers.
    >
    > character(len=30)::b="123.4"
    > real::a
    >
    > how can I assign 123.4 to a as a real number?
    >
    > Many thanks!


    In Fortran, you do not use functions for this operation, unlike in the
    C-like languages. Instead, you use "Internal I/O" statements, more specifically
    as "Internal Write" and "Internal Read" statements.

    An internal read converts from character to binary. An internal write
    converts from binary to character.

    Internal read and write statements work just like their counterparts that
    read from and write to external files. The only significant difference from the
    programmer's perspective is that you use a CHARACTER variable or array element
    in place of the Logical Unit Number (LUN). Everything else is the same,
    including format specifications and all of the other I/O keywords.

    Using your example, here is what you would do:

    Write (Unit=B, FMT="(F5.1)") A ! Converts value of A into CHARACTER variable
    B using F5.1 format edit descriptor.

    Read (Unit=B, FMT=*) A ! Converts from CHARACTER variable B into REAL
    variable A using list-directed formatting.

    Please read your Fortran documentation and/or textbook for further details
    on how this works.

    --
    Craig Dedo
    17130 W. Burleigh Place
    P. O. Box 423
    Brookfield, WI 53008-0423
    Voice: (262) 783-5869
    Fax: (262) 783-5928
    Mobile: (414) 412-5869
    E-mail: <cdedo@wi.rr.com> or <craig@ctdedo.com>


  5. Default Re: How to convert integers to characters?

    hermitian <iamwuxin@gmail.com> wrote:

    > But, is there any pre-defined internal functions could be used
    > directly?


    It is a bit hidden in the wrappings of the user-written functions that
    Terry showed, but the actual conversion is done by the read and write
    statements. I would actually recommend against using Terry's functions
    as is because they are unnecessarily nonstandard and compiler dependent.
    I'm also a bit puzzled by some of teh complication. In addition to that,
    critical parts are missing from the posted code. Those parts are
    probably in the non-posted misc.h file, which I can figure out how to
    reverse engineer, but that reverse engineering is more work than solving
    the originally stated problem from scratch.

    Look up internal i/o in any Fortran text. (Someone recently mentioned
    here having a Fortran text that didn't cover internal I/O. I don't nkow
    whether the text really didn't cover it or the poster just couln't find
    it. He never mentioned what text it was. It you do happen to find a
    textbook that doesn't cover it, throw that textbook out and tell us what
    ne it was.)

    Basically, an internal read/write looks just like an "ordinary" one,
    except that you substitute a character variable in lace of the unit
    number. The read/write is then from/to that character variable.

    As a simple example, using your stated problem

    character(len=30)::b="123.4"
    real::a

    read(b,*) a

    A reverse conversion would look like

    write(b,*) a

    No, there isn't a function version provided as part of the language. You
    can write your own easily enough as desired. If the language had one, by
    the time it had all the cpability of the read/write statements, it would
    also be as complicated as they are.

    --
    Richard Maine | Good judgement comes from experience;
    email: last name at domain . net | experience comes from bad judgement.
    domain: summertriangle | -- Mark Twain

  6. Default Re: How to convert integers to characters?

    Craig Dedo <cdedo@wi.rr.com> wrote:
    [about internal I/O]
    > you use a CHARACTER variable or array element


    Note that in Fortran 77, an array element *IS* a character variable
    (unless it is a constant, but that case happens to be disallowed here
    anyway). Thus the above statement is both more concise and more accurate
    if you omit the "or array element" part.

    This wasn't the case in f77, where array elements were a completely
    distinct category of things. But other parts of the post implicitly
    assume f90 or later (or f77 with extensions) anyway.

    --
    Richard Maine | Good judgement comes from experience;
    email: last name at domain . net | experience comes from bad judgement.
    domain: summertriangle | -- Mark Twain

  7. Default Re: How to convert integers to characters?

    Richard Maine <nospam@see.signature> wrote:

    > Craig Dedo <cdedo@wi.rr.com> wrote:
    > [about internal I/O]
    > > you use a CHARACTER variable or array element

    >
    > Note that in Fortran 77, an array element *IS* a character variable


    Oops. I see that I miswrote that, with the result that my post reads
    nonsensically. Let me be teh first to correct it. The above should read
    "in Fortran 90", or perhaps better "as of Fortran 90".

    > (unless it is a constant, but that case happens to be disallowed here
    > anyway). Thus the above statement is both more concise and more accurate
    > if you omit the "or array element" part.
    >
    > This wasn't the case in f77, where array elements were a completely
    > distinct category of things. But other parts of the post implicitly
    > assume f90 or later (or f77 with extensions) anyway.



    --
    Richard Maine | Good judgement comes from experience;
    email: last name at domain . net | experience comes from bad judgement.
    domain: summertriangle | -- Mark Twain

  8. Default Re: How to convert integers to characters?

    In message 29 Sep
    nospam@see.signature (Richard Maine) wrote:

    > hermitian <iamwuxin@gmail.com> wrote:
    >
    > > But, is there any pre-defined internal functions could be used
    > > directly?

    [snip]
    > In addition to that,
    > critical parts are missing from the posted code. Those parts are
    > probably in the non-posted misc.h file, which I can figure out how to
    > reverse engineer,

    [snip]

    My apologies to all I should have included "misc.h", and the relevant bits
    from that have been passed onto the OP. Just in case anyone needs to know,
    the code is F77, and has been used on countless systems and compilers for
    something like 15 years or so.

    Cheers,
    --
    Regards,
    Terry Duell


  9. Default Re: How to convert integers to characters?

    Thank you for all your kindly help, especially Richard Maine!

    Here I still have a problem that it seems the method you demonstrated
    did NOT work perfectly!

    Below is the code to convert a (a real variable) to b (a character)
    and then print out the length of b.

    ! Fortran 90 code begins
    program ex
    implicit none
    real::a=123.4
    character(len=10)::b
    write(b,*)a
    write(*,"(A8,1X,I3)")b, len_trim(b)
    end
    ! Fortran 90 code ends

    I use Debian Linux for i386 and have two fortran compilers installed,
    g95 and gfortran.

    (1) Being compiled by g95, the output is:
    123.4 6
    (2) Being compiled by gfortran, the output is an runtime error:
    At line 5 of file ex.f90
    Fortran runtime error: End of record

    The actually expected length of character variable b is 5, but neither
    6 nor an error!

    So, is there anyone can figure out and fix the problem?

    Many thanks!




  10. Default Re: How to convert integers to characters?

    I finally find out the problems!

    I didn't specify the formats of the data!

    Big mistake!

    On Sep 29, 3:29 pm, hermitian <iamwu...@gmail.com> wrote:
    > Thank you for all your kindly help, especially Richard Maine!
    >
    > Here I still have a problem that it seems the method you demonstrated
    > did NOT work perfectly!
    >
    > Below is the code to convert a (a real variable) to b (a character)
    > and then print out the length of b.
    >
    > ! Fortran 90 code begins
    > program ex
    > implicit none
    > real::a=123.4
    > character(len=10)::b
    > write(b,*)a
    > write(*,"(A8,1X,I3)")b, len_trim(b)
    > end
    > ! Fortran 90 code ends
    >
    > I use Debian Linux for i386 and have two fortran compilers installed,
    > g95 and gfortran.
    >
    > (1) Being compiled by g95, the output is:
    > 123.4 6
    > (2) Being compiled by gfortran, the output is an runtime error:
    > At line 5 of file ex.f90
    > Fortran runtime error: End of record
    >
    > The actually expected length of character variable b is 5, but neither
    > 6 nor an error!
    >
    > So, is there anyone can figure out and fix the problem?
    >
    > Many thanks!




+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. How to strip (convert) characters in a string
    By Application Development in forum Clipper
    Replies: 3
    Last Post: 12-26-2007, 09:39 AM
  2. Counting utf-8 characters -special characters
    By Application Development in forum Javascript
    Replies: 6
    Last Post: 09-19-2007, 09:55 AM
  3. Quick way to convert Integers to Binary in TCL
    By Application Development in forum TCL
    Replies: 3
    Last Post: 07-30-2007, 01:00 PM
  4. convert specials characters in a file
    By Application Development in forum awk
    Replies: 1
    Last Post: 01-13-2007, 07:09 AM