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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gfortran: loading hex into DATA statement CHARACTER variables

Status
Not open for further replies.

mikkelstuff

Technical User
Dec 26, 2016
2
US
I need to move existing code from the INTEL FORTAN ifort compiler to GNU gfortran.

The gfortran compiler does not like this coding:

CHARACTER Comma*1
C
DATA Comma/Z'2C'/

Anyway to fix this besides an ASCII implementation.

DATA Comma/','/

I have a large number of these which were fine using the Intel fortran compiler.
 
Well, it sounds like you already found a solution to your problem. What else are you looking for here?

Sources do require maintenance and yours requires at least modernization from f77 to f90/95...so, the way I see it, this is just one of the changes you need to make among many others. Most changes can be handled with a f77-to-f90 converter; others, you may have to take advantage of macros or search-and-replace in your editor.

Anyway, getting back to the issue presented in your post, do you see the problem? The code is effectively attempting to assign an integer (with default length surely greater than 1 byte) to a character variable, thus, the incompatibility.

Heck, I think "data" blocks are falling out of fashion, too. I would replace that "data" with an assignment statement:

comma = ','

or, if you want to preserve the hex expression/conversion:

comma = achar(Z'2C')

gsal






 
So I take it I will have to use the hex expression/conversion for non-ASCII characters such as

Trial=achar(Z'B6')

Thanks,
Clark
 
Yes - you do know that you can output in unicode on the console as well on Linux.
Code:
program uniq
    use iso_fortran_env
    implicit none
    integer, parameter :: unicode  = selected_char_kind ('ISO_10646')
          
    character(kind=unicode,  len=30) :: chinese

    ! create a unicode string
    chinese = unicode_'Ni Hao ' &
              // char (int (z'4F60'), unicode) &
              // char (int (z'597D'), unicode) &
              // unicode_'3/4 ' & char(int(z'BE', unicode)

    ! Output in unicode          
    open (output_unit, encoding='UTF-8')
    write (*,*) trim(chinese)
end program uniq
 
Following from the previous snippet, you can also use

unicode_'\u00be'

instead of char(int(z'be'), unicode) for the 3/4 but the program must be compiled with -fbackslash otherwise it doesn't work.
 
Hey, thanks for the unicode lesson.

By the way, I did not have to use the -fbadkslash flag (gfortran 4.8 or 6.3); but, noticed the "open(output_unit, enconding='UTF-8')" line is a must.
 
If I compile a program with the following

print *, '\u00be'

without -fbackslash, I get

\u00be

With -fbackslash, I get

¾
 
Oh, I understand now; like that much better.
Code:
program uni
    use iso_fortran_env
    implicit none
    integer, parameter :: u=selected_char_kind('ISO_10646')
    character(kind=u, len=30) :: chinese

    ! create a unicode string
    chinese = u_'Ni Hao: \u4F60 \u597D ; 3/4: \u00BE'

    ! Output in unicode
    open (output_unit, encoding='UTF-8')
    write (*,*) trim(chinese)
end program uni
 
You can also run this on windows but it is quite painful - you need to install the east asian fonts and change the code page (chcp 65001). It only works on the cmd prompt from Vista onwards. I spent about 3 hours trying to get it to work on XP but it just wasn't having it.

You can change it to the Chinese code page (chcp 54936 - not 936 as many articles/forums say) there is a spurious ? that appears after each character.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top