Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28383 Discussions

Write the Degree Symbol(UTF-8) to file

Jing1
Novice
3,301 Views

Hi,

Currently we can do this: open a text file and write degree symbol(°F) in FORTRAN, then read this file in C++ with ANSI mode.

But now if we read the same file in C++ with UTF-8 mode, we have trouble with degree symbol. Please refer to the attached screenshots.

We tried to add "encoding = 'UTF-8'" option when we open the file before writing the degree symbol in FOTRAN, it doesn't help.

So we have 2 questions:

1. Does Intel FORTRAN support encoding UTF-8 for text file, exp. for special characters -- degree symbol, square, cube etc.?

2. If yes, how should we write the degree symbol to text file in FORTRAN to let FORTRAN compiler knows this is UTF-8 mode, not ANSI mode?

Thanks.

 

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
3,282 Views

What I would suggest is creating a series of named (PARAMETER) character constants for the symbols you want. You could then write them in formatted I/O using an A format.

character(2), parameter :: utf_degree = char(int(Z'C2'))//char(int(Z'B0'))
character(2), parameter :: utf_squared = char(int(Z'C2'))//char(int(Z'B2'))
character(2), parameter :: utf_cubed = char(int(Z'C2'))//char(int(Z'B3'))
open (unit=1,file='E:\new2.txt', form='formatted', status='unknown')
write (1,'(A,A)') "Degree: ", utf_degree
write (1,'(A,A)') "Squared: ", utf_squared
write (1,'(A,A)') "Cubed: ", utf_cubed
close (1)
end

Screenshot 2021-08-23 141734.png

View solution in original post

8 Replies
Steve_Lionel
Honored Contributor III
3,288 Views

Intel Fortran does not currently support UTF-8 in files. But if you can figure out the UTF-8 encoding, you can write it as a series of bytes. For example:

 

 

open (unit=1,file='E:\new 2.txt', form='unformatted',access='stream',status='new')
write (1) '37',int(Z'B0C2',2)
close (1)
end

 

 

Screenshot 2021-08-23 140040.png

Steve_Lionel
Honored Contributor III
3,283 Views

What I would suggest is creating a series of named (PARAMETER) character constants for the symbols you want. You could then write them in formatted I/O using an A format.

character(2), parameter :: utf_degree = char(int(Z'C2'))//char(int(Z'B0'))
character(2), parameter :: utf_squared = char(int(Z'C2'))//char(int(Z'B2'))
character(2), parameter :: utf_cubed = char(int(Z'C2'))//char(int(Z'B3'))
open (unit=1,file='E:\new2.txt', form='formatted', status='unknown')
write (1,'(A,A)') "Degree: ", utf_degree
write (1,'(A,A)') "Squared: ", utf_squared
write (1,'(A,A)') "Cubed: ", utf_cubed
close (1)
end

Screenshot 2021-08-23 141734.png

Jing1
Novice
3,242 Views

Thanks a lot. This is really helpful!

 

0 Kudos
Jing1
Novice
3,164 Views

Hi Steve,

We are having trouble to use the same way to write these characters(fourth power, ≤, ≥) in FORTRAN to text file, because these characters' UNICODE(UTF-8) value has 3 bytes.

Here is our test code:

character(3), parameter :: utf_fourth = char( int( Z'E2' )) // char(int( Z'81' )) // char(int( Z'B4' )) ! fourth power

open (unit=1,file='E:\new2.txt', form='formatted', status='unknown')

write (1,'(A,A)') "Fourth: ", utf_fourth 

close (1)

end

 

But the test result is:

Jing1_0-1630434772261.png

 

 

0 Kudos
Steve_Lionel
Honored Contributor III
3,157 Views

Your test result doesn't match the program you show. When I compile and run the program, I get the proper "fourth" character.

character(3), parameter :: utf_fourth = char( int( Z'E2' )) // char(int( Z'81' )) // char(int( Z'B4' )) ! fourth power
character(3), parameter :: utf_lessequal = char(int(Z'E2')) // char(int(Z'89')) // char(int(Z'A4')) ! Less than or equal
character(3), parameter :: utf_greaterequal = char(int(Z'E2')) // char(int(Z'89')) // char(int(Z'A5')) ! Greaterthan or equal
open (unit=1,file='E:\new2.txt', form='formatted', status='unknown')

write (1,'(A,A)') "Fourth: ", utf_fourth 
write (1,'(A,A)') "Less or equal: ", utf_lessequal
write (1,'(A,A)') "Greater or equal: ", utf_greaterequal

close (1)

end

Screenshot 2021-08-31 143543.png

Jing1
Novice
3,124 Views

Thanks for quick response, Steve.

Sorry for the confusion, I uploaded the wrong screen shot.

I copied your test code and ran it, why did I get this for fourth power? Any idea or guess?

Jing1_1-1630450684917.png

 

0 Kudos
andrew_4619
Honored Contributor II
3,097 Views

I would guess because the font being used in your text editor does not have a definition for that code. I note that Steve opened in Notepad and you have some other program. 

0 Kudos
Steve_Lionel
Honored Contributor III
3,077 Views

Ah, that's Notepad++ which I also have. It defaults to Courier New font which doesn't have that character. Indeed, if I copy that character from Character Map and paste it into Notepad++, I get the block symbol.

Do this. Settings > Style Configurator... 

Language: Global Style, Style: Default Style. Change Font from Courier New to Consolas. While you're at it, you may want to change the font size from 10 to something larger (at least I did.) Save and Close.

Screenshot 2021-09-01 102241.png

Reply