Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Int2Str

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
0
0
DK
Hi,

What wouldn't I give for a Int2Str fortran function!

In thread214-1521223 mikrom shows how to make a function which returns a variable length character. The problem is that I do not know the length of the return string until a have 'written' it into a string and trimmed it - and I can do that until after the declarations.. any solutions?

Code:
    function int2Str(i)
        character(len=????) :: int2Str
        integer, intent(in) :: i
        
        character(20) :: c
        write (c,'(i20)') i
        int2Str = trim(adjustl(c))
    end function int2Str

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I don't know how to make the function result of variable length. But why not to use fixed length? You can then trim the resulting string.
Code:
[COLOR=#a020f0]program[/color] integer_to_string
  [COLOR=#2e8b57][b]integer[/b][/color] :: int_var
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]20[/color]) :: string_var

  int_var[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]12345[/color]
  [COLOR=#0000ff]! convert integer to string using[/color]
  string_var [COLOR=#804040][b]=[/b][/color] int2str(int_var)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a,a)'[/color])  [COLOR=#ff00ff]'result of int2str     ='[/color], string_var  
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a,i2)'[/color]) [COLOR=#ff00ff]'result length         ='[/color], [COLOR=#008080]len[/color](string_var)  
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a,i2)'[/color]) [COLOR=#ff00ff]'trimmed result length ='[/color], [COLOR=#008080]len_trim[/color](string_var)  
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a,i2)'[/color]) [COLOR=#ff00ff]'trimmed result length ='[/color], [COLOR=#008080]len[/color]([COLOR=#008080]trim[/color](string_var))  

[COLOR=#a020f0]contains[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]20[/color]) [COLOR=#a020f0]function[/color] int2str(num) 
     [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]):: num
     [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]20[/color]) :: str
     [COLOR=#0000ff]! convert integer to string using formatted write[/color]
     [COLOR=#804040][b]write[/b][/color](str, [COLOR=#ff00ff]'(i20)'[/color]) num
     int2str [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]adjustl[/color](str)
  [COLOR=#a020f0]end function[/color] int2str
[COLOR=#a020f0]end program[/color]
Code:
$ g95 int2str.f95 -o int2str

$ int2str
result of int2str     =12345               
result length         =20
trimmed result length = 5
trimmed result length = 5
 
Ok, that'll have to do. Thx.


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
The maximum length is the number of digits in a number of that size. For instance, in a integer*2, the maximum is 32768 which is 5. Add 1 for negative numbers so it is 6.

For a 32 bit number integer*4, it is 2147483647 which is 10 digits. Add 1 for negative numbers so it is 11.

If, like me, you cannot remember these figures, pop up the calculator in scientific mode. Switch to hex, and type 7FFF for integer*2, 7FFFFFFF for integer*4 and then switch to decimal. If your calculator can take it, 7FFFFFFFFFFFFFFF is the max for integer*8.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top