FC2ブログ

[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

コメントの投稿

非公開コメント

No title

No title

これすごい。試してみます!(^<^)

No title

>おいプログラマ誰よ?

ナイショ!

なおなんらかの個人情報が記載されたコメントは
すべてJCSAS管理部のほうで削除させていただきます。
よろしくお願い申し上げます。

No title

おいプログラマ誰よ?

No title

なんかすごい

No title

なお、プログラマ情報はナイショです ><
どこかの誰かさんです

No title

当ページで公開しているプログラム群の使用はフリーです。
一応バグはないものと考えておりますが、
自己責任の下お使いください。

当ページで公開するプログラムを使用した際に生じる責任は使用者に帰するものとします。

JCSAS管理部
プロフィール

jcsas

Author:jcsas
みんなでがんばります!

最新記事
最新コメント
月別アーカイブ
カテゴリ
アクセスランキング
[ジャンルランキング]
コンピュータ
619位 / 13835人中
アクセスランキングを見る>>

[サブジャンルランキング]
プログラミング
85位 / 2573人中
アクセスランキングを見る>>
来場者数
検索フォーム
リンク
QRコード
QR