LoginSignup
18
9

More than 5 years have passed since last update.

nkfでCP932←→UTF-8の変換をする

Posted at

入力文字コードを自動判別させる場合

デフォルトではCP932←→UnicodeではなくShift_JIS←→Unicodeの変換をしてしまう。
CP932←→Unicodeの変換をさせるには--cp932オプションを指定すれば良い。

# CP932 → UTF-8
$ nkf -w --cp932 cp932.txt > utf8.txt

# UTF-8 → CP932
$ nkf -s --cp932 utf8.txt > cp932.txt

ただしこの--cp932man nkfに説明が書いていない。

入力文字コードを明示的に指定する場合

# CP932 → UTF-8
nkf --ic=CP932 --oc=UTF-8 cp932.txt > utf8.txt

# UTF-8 → CP932
nkf --ic=UTF-8 --oc=CP932 utf8.txt > cp932.txt

参考:cp932からunicodeへ変換した場合に異なる文字

          CP932→Unicode     Shift_JIS→Unicode
0x8160    ~    U+FF5E      〜    U+301C
0x8161    ∥    U+2225      ‖    U+2016
0x817C    -    U+FF0D      −    U+2212
0x8191    ¢    U+FFE0      ¢    U+00A2
0x8192    £    U+FFE1      £    U+00A3
0x81CA    ¬    U+FFE2      ¬    U+00AC

https://sites.google.com/site/fudist/Home/vim-nihongo-ban/mojibake/utf8-cp932conv

18
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
9