[fortran] 全角数字を半角数字に変換するプログラム部品
全角数字「123」などを半角数字「123」などに変換するプログラム部品(Fortran90)です。
これを応用すれば、ある部分を変更するだけで他の文字などへも変更できます。
どうぞご利用ください。
!tm -------------------------------------------------------
subroutine chg_fd2hd(ifile, ofile)
!tm 全角数字 -> 半角数字
integer (8), parameter :: in_unit = 9917
integer (8), parameter :: out_unit = 9918
character (len=2) :: moji
character (*) :: ifile, ofile
open (in_unit, file=ifile, access='stream', form='formatted', status='unknown')
open (out_unit, file=ofile, access='stream', form='formatted', status='unknown')
do
read (in_unit, '(a)', advance='no', eor=100, end=110) moji(1:1) ! read the first byte
if (ichar(moji(1:1))>127) then ! assume two-byte character if 1st byte is > 127
read (in_unit, '(a)', advance='no') moji(2:2) ! read the 2nd byte
if (moji(1:1)==char(130) .and. moji(2:2)>=char(79) .and. moji(2:2)<=char(88)) then ! if moji is full-width digit
write (out_unit, '(a)', advance='no') char(48+ichar(moji(2:2))-79) ! convert to half-width digiti
else
write (out_unit, '(a)', advance='no') moji ! two-byte char output as is
end if
else
write (out_unit, '(a)', advance='no') moji(1:1) ! single byte char output as is
end if
cycle
100 write (out_unit, '()') ! new record
end do
110 continue
close (in_unit)
close (out_unit)
end subroutine chg_fd2hd
これを応用すれば、ある部分を変更するだけで他の文字などへも変更できます。
どうぞご利用ください。
!tm -------------------------------------------------------
subroutine chg_fd2hd(ifile, ofile)
!tm 全角数字 -> 半角数字
integer (8), parameter :: in_unit = 9917
integer (8), parameter :: out_unit = 9918
character (len=2) :: moji
character (*) :: ifile, ofile
open (in_unit, file=ifile, access='stream', form='formatted', status='unknown')
open (out_unit, file=ofile, access='stream', form='formatted', status='unknown')
do
read (in_unit, '(a)', advance='no', eor=100, end=110) moji(1:1) ! read the first byte
if (ichar(moji(1:1))>127) then ! assume two-byte character if 1st byte is > 127
read (in_unit, '(a)', advance='no') moji(2:2) ! read the 2nd byte
if (moji(1:1)==char(130) .and. moji(2:2)>=char(79) .and. moji(2:2)<=char(88)) then ! if moji is full-width digit
write (out_unit, '(a)', advance='no') char(48+ichar(moji(2:2))-79) ! convert to half-width digiti
else
write (out_unit, '(a)', advance='no') moji ! two-byte char output as is
end if
else
write (out_unit, '(a)', advance='no') moji(1:1) ! single byte char output as is
end if
cycle
100 write (out_unit, '()') ! new record
end do
110 continue
close (in_unit)
close (out_unit)
end subroutine chg_fd2hd