unicode(7f) - [FORTRAN] UNICODE example
Description
Author
Example
Support for ISO 10646 (Unicode) is not required for Fortran 2003 any more than it is required for C, but the Standard does specify how it should be done.The new intrinsic function SELECTED_CHAR_KIND(3f) can be used to request ASCII characters or ISO 10646 characters on any processor that supports them. The ISO 10646 characters that result use the UCS-4 encoding (4 bytes per character); this uses more memory than, say, UCS-2 (which uses 2 bytes for most characters), but avoids the complication of surrogates. It also means that the Fortran substring operations and searching functions will operate correctly and efficiently even on characters outside the Basic Multilingual Plane.
Recent changes to the Standard have seen the addition of support for numeric and logical formatting to Unicode characters, conversion between ASCII, default, and Unicode characters, and support for UTF-8 formatted files. The latter is indicated by the new encoding=specifier, for example:
open(...,encoding=UTF-8...)
Based on an article on "ISO 10646 support", Dr. DobbsMalcolm is a principal technical consultant for the Numerical Algorithms Group, and an active member of the J3 and WG5 Fortran standardization committees. He can be contacted at malcolmnag.co.uk.
Example program 1
program character_kind use iso_fortran_env implicit none integer, parameter :: ascii = selected_char_kind ("ascii") integer, parameter :: ucs4 = selected_char_kind (ISO_10646)character(kind=ascii, len=26) :: alphabet character(kind=ucs4, len=30) :: hello_world
alphabet = ascii_"abcdefghijklmnopqrstuvwxyz" hello_world = ucs4_Hello World and Ni Hao -- & // char (int (z4F60), ucs4) & // char (int (z597D), ucs4)
write (*,*) alphabet
open (output_unit, encoding=UTF-8) write (*,*) trim (hello_world) end program character_kind
Nemo Release 3.1 | unicode_example1 (1) | February 23, 2025 |