[topic] 文字コードの変換

1.shift-jisで書かれた文字列をeuc-jpの文字列に変換して返す関数を作ってください。

2.shift-jisで書かれたファイルをeuc-jpに変換してファイル出力するしてください。ただし1で作成した関数を利用せずに。

何気にどう書く?orgに文字コード系の話が出てなかったような気がしたので投稿してみる。

2は言語仕様レベルでさくっといける場合(perlのencodingとか)でお願いします。
読み込みや書き込みのレベルで、文字コードを考えないでスパッといけるのが望ましい。

Posted feedbacks - Common Lisp

よく知らなかったので調べながら……

文字列の変換は shiro さんの #4628 と同様にバイト列の変換にしています。ファイル入出力の際は :external-format を指定することで文字コード指定ができます。

いずれも処理系依存です。CLISP 2.38 で動作確認しました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
;;; 1
(defun sjis-to-euc-jp (bytes)
  (convert-string-to-bytes
   (convert-string-from-bytes bytes charset:shift-jis)
   charset:euc-jp))

;;; 2
(defun sjis-to-euc-jp-file (from to)
  (with-open-file (in from :direction :input
                     :external-format charset:shift-jis)
    (with-open-file (out to :direction :output
                         :external-format charset:euc-jp)
      (do () ((null (listen in)))
        (write-line (read-line in) out)))))

Index

Feed

Other

Link

Pathtraq

loading...