[topic] 文字コードの変換
Posted feedbacks - Scheme
Gaucheでは、「shift jisで書かれた文字列」とか「eucjpで書かれた文字列」という言い方はちょっと微妙です。エンコーディングは原則として処理系の「外部」にあるもので、言語レベルでの文字列は内部エンコーディングで統一されているので(入出力時や外部ライブラリ呼び出し時に変換)。ただ、文字列を無理やりバイト列として解釈させることが(今は)できるので、それを使うと一応「shift jisで書かれた文字列」みたいなものを扱うことはできます。#*"..." というのが「生の」文字列の表記です。
gosh> (ces-convert "いろは" 'utf-8 'sjis)
#*"\x82\xa2\x82\xeb\x82\xcd"
gosh> (sjis2eucjp #*"\x82\xa2\x82\xeb\x82\xcd")
#*"\xa4\xa4\xa4\xed\xa4\xcf"
gosh> (ces-convert #*"\xa4\xa4\xa4\xed\xa4\xcf" 'eucjp 'utf-8)
"いろは"
ただ、こういう扱いは意味的にすっきりしないので、将来はバイトベクタとして扱ってもらうようにするかもしれません。
gosh> (ces-convert "いろは" 'utf-8 'sjis)
#*"\x82\xa2\x82\xeb\x82\xcd"
gosh> (sjis2eucjp #*"\x82\xa2\x82\xeb\x82\xcd")
#*"\xa4\xa4\xa4\xed\xa4\xcf"
gosh> (ces-convert #*"\xa4\xa4\xa4\xed\xa4\xcf" 'eucjp 'utf-8)
"いろは"
ただ、こういう扱いは意味的にすっきりしないので、将来はバイトベクタとして扱ってもらうようにするかもしれません。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | (use gauche.charconv)
;; 1
(define (sjis2eucjp str)
(ces-convert str 'sjis 'eucjp))
;; 2
(define (sjis2eucjp-file infile outfile)
(call-with-input-file infile
(lambda (in)
(call-with-output-file outfile
(lambda (out) (copy-port in out))
:encoding 'eucjp))
:encoding 'sjis))
|



ところてん
#4620()
Rating1/1=1.00
[ reply ]