LoginSignup
37
25

More than 5 years have passed since last update.

NetCore でSJISエンコードを扱う

Last updated at Posted at 2019-03-26

.Net Coreで、GetEncoding("Shift_JIS")とするとエラーになる。

var sjisEnc = Encoding.GetEncoding("Shift_JIS");

エンコードリストにShift_JISが無いことに起因する。

Shift_JISが使えるように以下をNugetからインストールする。
NuGet Gallery | System.Text.Encoding.CodePages 4.5.1

sJISを使う前に、Encoding.RegisterProvider()をコールする事でSJISが使えるようになる。


using System.Text;


Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var sjisEnc = Encoding.GetEncoding("Shift_JIS");

おまけ

サポートしているすべてのエンコーディングを取得する

//サポートしているすべてのエンコーディングを取得する
var eis = System.Text.Encoding.GetEncodings();
foreach (System.Text.EncodingInfo ei in eis)
{
    Console.WriteLine("{0}\t{1}\t{2}", ei.DisplayName, ei.CodePage, ei.Name);
}

結果

Unicode 1200    utf-16
Unicode (Big-Endian)    1201    utf-16BE
Unicode (UTF-32)        12000   utf-32
Unicode (UTF-32 Big-Endian)     12001   utf-32BE
US-ASCII        20127   us-ascii
Western European (ISO)  28591   iso-8859-1
Unicode (UTF-7) 65000   utf-7
Unicode (UTF-8) 65001   utf-8
37
25
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
37
25