Comment detail

文字コードの変換 (Nested Flatten)
Squeak Smalltalk で。

Squeak3.8 以降に組み込みの多言語化機構では、TextConverter のサブクラスに定義された各種文字コード向けコンバータクラスのインスタンスを介して相互に変換できます。

ファイル入出力については、読み書きに用いるファイルストリームの converter 属性にあらかじめ適切な文字コード向けのコンバータを指定しておきます。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| sjisToEucStr result |
sjisToEucStr := [:sjisStr |
    | str |
    str := sjisStr convertFromEncoding: #sjis.
    str convertToEncoding: #eucjp].

result := sjisToEucStr value: ('あいう' convertToEncoding: #sjis).
^result asByteArray   "=> a ByteArray(164 162 164 164 164 166) "


| sjisToEucFileNamed result line |
sjisToEucFileNamed := [:in :out |
    in := FileStream oldFileNamed: 'in.sjis'.
    in converter: (TextConverter newForEncoding: #sjis).
    out := FileStream newFileNamed: 'out.euc'.
    out converter: (TextConverter newForEncoding: #eucjp).
    [(line := in nextLine) notNil] whileTrue: [out nextPutAll: line; cr].
    in close.  out close].

sjisToEucFileNamed value: 'in.sjis' value: 'out.euc'.
result := FileStream oldFileNamed: 'out.euc'.
result binary.
^result contentsOfEntireFile   "=> a ByteArray(164 162 164 164 164 166 13) "

Index

Feed

Other

Link

Pathtraq

loading...