「C Sharp 文字コード変換」の版間の差分
ナビゲーションに移動
検索に移動
| 2行目: | 2行目: | ||
[[C Sharp]] | [[Category:文字化け]] | [[C Sharp]] | [[Category:文字化け]] | ||
===Shift_Jisで保存されたファイルを読み込んでUTF-8に変換する=== | ===Shift_Jisで保存されたファイルを読み込んでUTF-8に変換する=== | ||
| − | + | <pre> | |
| − | |||
| − | |||
| − | |||
Encoding srcEncoding = Encoding.GetEncoding("shift_jis"); | Encoding srcEncoding = Encoding.GetEncoding("shift_jis"); | ||
Encoding dstEncoding = Encoding.UTF8; | Encoding dstEncoding = Encoding.UTF8; | ||
| 23行目: | 20行目: | ||
} | } | ||
} | } | ||
| + | </pre> | ||
| + | |||
| + | *以下のようなエラーが出る場合 | ||
| + | <pre> | ||
| + | System.ArgumentException: ''Shift_JIS' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. ' | ||
| + | </pre> | ||
| + | [[File:Cshart_shift_jis.png|600px]] | ||
| + | <pre> | ||
| + | using System.Text; | ||
| + | Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | ||
| + | </pre> | ||
2020年12月27日 (日) 03:48時点における最新版
C# 文字コード変換
C Sharp |
Shift_Jisで保存されたファイルを読み込んでUTF-8に変換する
Encoding srcEncoding = Encoding.GetEncoding("shift_jis");
Encoding dstEncoding = Encoding.UTF8;
using (var reader = new Stream[[R]]eader(file, srcEncoding))
{
string line = null;
while ((line = reader.[[R]]eadLine()) != null)
{
String convertedLine =
dstEncoding.GetString(
System.Text.Encoding.Convert(
srcEncoding,
dstEncoding,
srcEncoding.GetBytes(line)));
Debug.WriteLine(convertedLine);
}
}
- 以下のようなエラーが出る場合
System.ArgumentException: ''Shift_JIS' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. '
using System.Text; Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
© 2006 矢木浩人