文字の変換

 投稿者:山中和義  投稿日:2009年 5月12日(火)13時39分43秒
              ひらがな
             ↑↓
 半角カタカナ → 全角カタカナ
        ←
 半角数字     全角数字
 半角英字     全角英字
 半角記号     全角記号

を行うサブルーチンをつくってみました。

DECLARE EXTERNAL FUNCTION StrConv.ASC$ ,StrConv.JIS$ !半角、全角
DECLARE EXTERNAL FUNCTION StrConv.ToHiragana$ ,StrConv.ToKatakana$ !ひらがな、カタカナ


LET t$="BASICプログラミングーぅ~!"
PRINT ASC$(t$) !半角へ
PRINT JIS$(ASC$(t$)) !全角へ

PRINT ToKatakana$("abcXYZあいうカキク890 *?")
PRINT ToHiragana$("abcXYZあいうカキク890 *?")

END


MODULE StrConv !文字の変換

!「半角 → 全角」対応表 ※JISコード(JIS X 0201)順
DATA " "," " !32
DATA "!","!" !33
DATA """","”" !34
DATA "#","#" !35
DATA "$","$" !36
DATA "%","%" !37
DATA "&","&" !38
DATA "'","’" !39
DATA "(","(" !40
DATA ")",")" !41
DATA "*","*" !42
DATA "+","+" !43
DATA ",","," !44
DATA "-","-" !45
DATA ".","." !46
DATA "/","/" !47
DATA "0","0" !48
DATA "1","1" !49
DATA "2","2" !50
DATA "3","3" !51
DATA "4","4" !52
DATA "5","5" !53
DATA "6","6" !54
DATA "7","7" !55
DATA "8","8" !56
DATA "9","9" !57
DATA ":",":" !58
DATA ";",";" !59
DATA "<","<" !60
DATA "=","=" !61
DATA ">",">" !62
DATA "?","?" !63
DATA "@","@" !64
DATA "A","A" !65
DATA "B","B" !66
DATA "C","C" !67
DATA "D","D" !68
DATA "E","E" !69
DATA "F","F" !70
DATA "G","G" !71
DATA "H","H" !72
DATA "I","I" !73
DATA "J","J" !74
DATA "K","K" !75
DATA "L","L" !76
DATA "M","M" !77
DATA "N","N" !78
DATA "O","O" !79
DATA "P","P" !80
DATA "Q","Q" !81
DATA "R","R" !82
DATA "S","S" !83
DATA "T","T" !84
DATA "U","U" !85
DATA "V","V" !86
DATA "W","W" !87
DATA "X","X" !88
DATA "Y","Y" !89
DATA "Z","Z" !90
DATA "[","[" !91
DATA "\","¥" !92
DATA "]","]" !93
DATA "^","^" !94
DATA "_","_" !95
DATA "`","‘" !96
DATA "a","a" !97
DATA "b","b" !98
DATA "c","c" !99
DATA "d","d" !100
DATA "e","e" !101
DATA "f","f" !102
DATA "g","g" !103
DATA "h","h" !104
DATA "i","i" !105
DATA "j","j" !106
DATA "k","k" !107
DATA "l","l" !108
DATA "m","m" !109
DATA "n","n" !110
DATA "o","o" !111
DATA "p","p" !112
DATA "q","q" !113
DATA "r","r" !114
DATA "s","s" !115
DATA "t","t" !116
DATA "u","u" !117
DATA "v","v" !118
DATA "w","w" !119
DATA "x","x" !120
DATA "y","y" !121
DATA "z","z" !122
DATA "{","{" !123
DATA "|","|" !124
DATA "}","}" !125
DATA "~","~" !126

DATA "。","。" !161
DATA "「","「" !162
DATA "」","」" !163
DATA "、","、" !164
DATA "・","・" !165
DATA "ヲ","ヲ" !166
DATA "ァ","ァ" !167
DATA "ィ","ィ" !168
DATA "ゥ","ゥ" !169
DATA "ェ","ェ" !170
DATA "ォ","ォ" !171
DATA "ャ","ャ" !172
DATA "ュ","ュ" !173
DATA "ョ","ョ" !174
DATA "ッ","ッ" !175
DATA "ー","ー" !176
DATA "ア","ア" !177
DATA "イ","イ" !178
DATA "ウ","ウ" !179
DATA "エ","エ" !180
DATA "オ","オ" !181
DATA "カ","カ" !182
DATA "キ","キ" !183
DATA "ク","ク" !184
DATA "ケ","ケ" !185
DATA "コ","コ" !186
DATA "サ","サ" !187
DATA "シ","シ" !188
DATA "ス","ス" !189
DATA "セ","セ" !190
DATA "ソ","ソ" !191
DATA "タ","タ" !192
DATA "チ","チ" !193
DATA "ツ","ツ" !194
DATA "テ","テ" !195
DATA "ト","ト" !196
DATA "ナ","ナ" !197
DATA "ニ","ニ" !198
DATA "ヌ","ヌ" !199
DATA "ネ","ネ" !200
DATA "ノ","ノ" !201
DATA "ハ","ハ" !202
DATA "ヒ","ヒ" !203
DATA "フ","フ" !204
DATA "ヘ","ヘ" !205
DATA "ホ","ホ" !206
DATA "マ","マ" !207
DATA "ミ","ミ" !208
DATA "ム","ム" !209
DATA "メ","メ" !210
DATA "モ","モ" !211
DATA "ヤ","ヤ" !212
DATA "ユ","ユ" !213
DATA "ヨ","ヨ" !214
DATA "ラ","ラ" !215
DATA "リ","リ" !216
DATA "ル","ル" !217
DATA "レ","レ" !218
DATA "ロ","ロ" !219
DATA "ワ","ワ" !220
DATA "ン","ン" !221
DATA "゙","゛" !222
DATA "゚","゜" !223


!「全角カタカナ → 半角カタカナ」の対応表
DATA "ァ","ァ", "ア","ア", "ィ","ィ", "イ","イ", "ゥ","ゥ", "ウ","ウ", "ェ","ェ", "エ","エ", "ォ","ォ", "オ","オ"
DATA "カ","カ", "ガ","ガ", "キ","キ", "ギ","ギ", "ク","ク", "グ","グ", "ケ","ケ", "ゲ","ゲ", "コ","コ", "ゴ","ゴ"
DATA "サ","サ", "ザ","ザ", "シ","シ", "ジ","ジ", "ス","ス", "ズ","ズ", "セ","セ", "ゼ","ゼ", "ソ","ソ", "ゾ","ゾ"
DATA "タ","タ", "ダ","ダ", "チ","チ", "ヂ","ヂ", "ッ","ッ", "ツ","ツ", "ヅ","ヅ", "テ","テ", "デ","デ", "ト","ト", "ド","ド"
DATA "ナ","ナ", "ニ","ニ", "ヌ","ヌ", "ネ","ネ", "ノ","ノ"
DATA "ハ","ハ", "バ","バ", "パ","パ", "ヒ","ヒ", "ビ","ビ", "ピ","ピ", "フ","フ", "ブ","ブ", "プ","プ", "ヘ","ヘ", "ベ","ベ", "ペ","ペ", "ホ","ホ", "ボ","ボ", "ポ","ポ"
DATA "マ","マ", "ミ","ミ", "ム","ム", "メ","メ", "モ","モ"
DATA "ャ","ャ", "ヤ","ヤ", "ュ","ュ", "ユ","ユ", "ョ","ョ", "ヨ","ヨ"
DATA "ラ","ラ", "リ","リ", "ル","ル", "レ","レ", "ロ","ロ"
DATA "ヮ","ワ", "ワ","ワ", "ヰ","イ", "ヱ","エ", "ヲ","ヲ", "ン","ン", "ヴ","ヴ", "ヵ","カ", "ヶ","ケ"


SHARE STRING JisTBL$(0 TO 255), KataTBL$(9505 TO 9590)
DO
   READ IF MISSING THEN EXIT DO : a$,aa$ !インデックス、変換値
   IF ORD(a$)<256 THEN
      LET JisTBL$(ORD(a$))=aa$ !ハッシュ表をつくる
   ELSE
      LET KataTBL$(ORD(a$))=aa$
   END IF
LOOP
!-------------------- ここまでが初期処理


つづく
 

Re: 文字の変換

 投稿者:山中和義  投稿日:2009年 5月12日(火)13時42分8秒
  > No.374[元記事へ]

つづき

!関数、サブルーチンの定義

PUBLIC FUNCTION ASC$
EXTERNAL FUNCTION ASC$(x$) !全角文字を半角文字に変換する
   LET xx$=""
   FOR i=1 TO LEN(x$) !対象の文字列について
      LET t=ORD(x$(i:i))

      SELECT CASE t
      CASE 9008 TO 9017 !数字0~9
         LET w$=CHR$(t-ORD("0")+ORD("0"))

      CASE 9025 TO 9050 !英字A~Z
         LET w$=CHR$(t-ORD("Z")+ORD("Z"))
      CASE 9057 TO 9082 !英字a~z
         LET w$=CHR$(t-ORD("z")+ORD("z"))

      CASE 9505 TO 9590 !カタカナ
         LET w$=KataTBL$(t) !変換表を参照して置き換える

      CASE 8481 !記号
         LET w$=" "
      CASE 8490
         LET w$="!"
      CASE 8521
         LET w$=""""
      CASE 8564
         LET w$="#"
      CASE 8560
         LET w$="$"
      CASE 8563
         LET w$="%"
      CASE 8565
         LET w$="&"
      CASE 8519
         LET w$="'"
      CASE 8522
         LET w$="("
      CASE 8523
         LET w$=")"
      CASE 8566
         LET w$="*"
      CASE 8540
         LET w$="+"
      CASE 8484
         LET w$=","
      CASE 8541
         LET w$="-"
      CASE 8485
         LET w$="."
      CASE 8511
         LET w$="/"
      CASE 8487
         LET w$=":"
      CASE 8488
         LET w$=";"
      CASE 8547
         LET w$="<"
      CASE 8545
         LET w$="="
      CASE 8548
         LET w$=">"
      CASE 8489
         LET w$="?"
      CASE 8567
         LET w$="@"
      CASE 8526
         LET w$="["
      CASE 8559
         LET w$="\"
      CASE 8527
         LET w$="]"
      CASE 8496
         LET w$="^"
      CASE 8498
         LET w$="_"
      CASE 8518
         LET w$="`"
      CASE 8528
         LET w$="{"
      CASE 8515
         LET w$="|"
      CASE 8529
         LET w$="}"
      CASE 8513
         LET w$="~"

      CASE 8483 !記号 ※カタカナ
         LET w$="。"
      CASE 8534
         LET w$="「"
      CASE 8535
         LET w$="」"
      CASE 8482
         LET w$="、"
      CASE 8486
         LET w$="・"
      CASE 8508
         LET w$="ー"
      CASE 8491
         LET w$="゙"
      CASE 8492
         LET w$="゚"

      CASE ELSE
         LET w$=CHR$(t) !そのまま
      END SELECT

      LET xx$=xx$ & w$
   NEXT i
   LET ASC$=xx$
END FUNCTION

PUBLIC FUNCTION JIS$
EXTERNAL FUNCTION JIS$(x$) !半角文字を全角文字に変換する
   LET xx$=""
   LET i=1
   DO WHILE i<=LEN(x$) !対象文字を順に調べる
      LET t=ORD(x$(i:i))

      SELECT CASE t
      CASE 32 TO 126 !記号・英数字なら
         LET xx$=xx$ & JisTBL$(t) !変換表を参照して置き換える
      CASE 161 TO 223 !半角カタカナなら
         IF i<LEN(x$) THEN LET tt=ORD(x$(i+1:i+1)) ELSE LET tt=0
         IF tt=222 AND ( (t>=182 AND t<=196) OR (t>=202 AND t<=206) ) THEN !カ~ト、ハ~ホの濁点
            LET xx$=xx$ & CHR$(ORD(JisTBL$(t))+1) !1文字とする
            LET i=i+1
         ELSEIF tt=223 AND (t>=202 AND t<=206) THEN !ハ~ホの半濁点
            LET xx$=xx$ & CHR$(ORD(JisTBL$(t))+2)
            LET i=i+1
         ELSE
            LET xx$=xx$ & JisTBL$(t)
         END IF
      CASE ELSE !それ以外なら
         LET xx$=xx$ & CHR$(t) !そのまま
      END SELECT

      LET i=i+1 !次へ
   LOOP

   LET JIS$=xx$
END FUNCTION

PUBLIC FUNCTION ToHiragana$
EXTERNAL FUNCTION ToHiragana$(x$) !全角カタカナをひらがなに変換する
   LET xx$=x$
   FOR i=1 TO LEN(x$) !対象文字を順に調べる
      LET t=ORD(x$(i:i))
      IF t<ORD("ァ") OR t>ORD("ン") THEN
      ELSE
         LET xx$(i:i)=CHR$(t-ORD("ァ")+ORD("ぁ"))
      END IF
   NEXT i
   LET ToHiragana$=xx$
END FUNCTION

PUBLIC FUNCTION ToKatakana$
EXTERNAL FUNCTION ToKatakana$(x$) !ひらがなを全角カタカナに変換する
   LET xx$=x$
   FOR i=1 TO LEN(x$) !対象文字を順に調べる
      LET t=ORD(x$(i:i))
      IF t<ORD("ぁ") OR t>ORD("ん") THEN
      ELSE
         LET xx$(i:i)=CHR$(t-ORD("ぁ")+ORD("ァ"))
      END IF
   NEXT i
   LET ToKatakana$=xx$
END FUNCTION

END MODULE

 

戻る