差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
dotnet:encoding [2009/01/19 10:35] – nakayama | dotnet:encoding [2019/05/18 02:23] (現在) – 外部編集 非ログインユーザー | ||
---|---|---|---|
行 2: | 行 2: | ||
===== テキストの内部表現とエンコーダ/ | ===== テキストの内部表現とエンコーダ/ | ||
.NET Framework がテキストを保持する際の内部表現は Unicode UTF-16 である。 | .NET Framework がテキストを保持する際の内部表現は Unicode UTF-16 である。 | ||
- | 例えば、Shift-JIS形式のファイルからデータを読み込む場合、デコーダはShift-JISを内部表現のUTF-16に変換する。 | + | 例えば、Shift-JIS形式のファイルからデータを読み込む場合、Shift-JISデコーダはShift-JISを内部表現のUTF-16に変換する。 |
- | また、Shift-JIS形式のファイルにデータを書き込む場合、エンコーダは内部形式のUTF-16をShift-JISに変換する。 | + | また、Shift-JIS形式のファイルにデータを書き込む場合、エンコーダは内部形式のUTF-16をShift-JISに変換する。\\ |
+ | \\ | ||
+ | 具体的には、StreamReader はファイルを内部バッファに読み込み、encoding.GetDecoder() にて取得した decoder を利用して、バッファのデータを decoder.GetChars() を使用しデコードする。\\ | ||
+ | 一方 StreamWriter は encoding.GetEncoder() にて取得した encoder を利用して、内部バッファのデータを Flush する時に encoder.GetBytes() を使ってエンコードし stream に Write して Flush する。\\ | ||
+ | \\ | ||
+ | 参考文献: | ||
+ | [[http:// | ||
+ | [[http:// | ||
===== エンコーダ/ | ===== エンコーダ/ | ||
Encoding クラスから派生した ASCIIEncoding や UTF8Encoding は、エンコーダ/ | Encoding クラスから派生した ASCIIEncoding や UTF8Encoding は、エンコーダ/ | ||
- | そのため、文字コード変換(エンコード/ | + | そのため、文字コード変換を行う処理では、Encodingを指定することによってエンコード/デコードの動作を指定できる。 |
- | ===== Encoding派生クラス ===== | + | |
- | ^クラス名^ | + | ===== 頻繁に利用されるEncoding派生クラス ===== |
- | |ASCIIEncoding| | + | ^クラス名^説明^ |
+ | |ASCIIEncoding|Unicode文字を1個の7ビットASCII文字としてエンコードする。このエンコーディングは、U+0000~U+007Fまでの文字値だけをサポートする。コード ページは20127。\\ Encoding.ASCIIプロパティを通じても使用できる。| | ||
+ | |UTF7Encoding|UTF-7エンコーディングを使用してUnicode文字をエンコードする。このエンコーディングは、すべてのUnicode文字値をサポートしている。コード ページは65000。\\ Encoding.UTF7プロパティを通じても使用できる。| | ||
+ | |UTF8Encoding|UTF-8エンコーディングを使用してUnicode文字をエンコードする。このエンコーディングは、すべてのUnicode文字値をサポートしている。コード ページは65001。\\ Encoding.UTF8プロパティを通じて使用できる。| | ||
+ | |UnicodeEncoding|UTF-16エンコーディングを使用してUnicode文字をエンコードする。リトル エンディアン(コード ページ 1200)とビッグ エンディアン(コード ページ 1201)の両方のバイト順をサポートしている。\\ Encoding.Unicodeプロパティ、および Encoding.BigEndianUnicodeプロパティを通じても使用できる。| | ||
+ | |UTF32Encoding|UTF-32エンコーディングを使用してUnicode文字をエンコードする。リトル エンディアン(コード ページ 65005)とビッグ エンディアン(コード ページ 65006)の両方のバイト順をサポートしている。\\ Encoding.UTF32プロパティを通じても使用できる。| | ||
+ | ===== 頻繁に利用されるエンコーディング ===== | ||
+ | ^文字コード^エンコーディング取得方法^備考^ | ||
+ | |ASCII|Encoding.ASCII| | | ||
+ | |Shift-JIS|Encoding.GetEncoding(" | ||
+ | |EUC-JP|Encoding.GetEncoding(" | ||
+ | |UTF-8|Encoding.UTF8|StreamReader/ | ||
+ | |||
+ | ===== ファイルの読み込み ===== | ||
+ | Shift-JISファイルの読み込みの例\\ | ||
+ | [VB] | ||
+ | <code vbnet> | ||
+ | Imports System | ||
+ | Imports System.IO | ||
+ | Imports System.Text | ||
+ | |||
+ | Module SampleAppVB | ||
+ | Sub Main() | ||
+ | ' Shift-JISファイルを開く | ||
+ | Using sr As StreamReader = _ | ||
+ | New StreamReader(" | ||
+ | ' コンソール出力 | ||
+ | Console.WriteLine(sr.ReadToEnd()) | ||
+ | End Using | ||
+ | End Sub | ||
+ | End Module | ||
+ | </ | ||
+ | [C#] | ||
+ | <code csharp> | ||
+ | using System; | ||
+ | using System.IO; | ||
+ | using System.Text; | ||
+ | |||
+ | namespace SampleApplicationCS | ||
+ | { | ||
+ | public class SampleAppCS | ||
+ | { | ||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | // Shift-JISファイルを開く | ||
+ | using (StreamReader sr = | ||
+ | new StreamReader(" | ||
+ | { | ||
+ | // コンソール出力 | ||
+ | Console.WriteLine(sr.ReadToEnd()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | [C++] | ||
+ | <code cpp> | ||
+ | using namespace System; | ||
+ | using namespace System:: | ||
+ | using namespace System:: | ||
+ | |||
+ | int main(array< | ||
+ | { | ||
+ | StreamReader sr(" | ||
+ | |||
+ | // コンソール出力 | ||
+ | Console:: | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |