1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#プログラミングメモ

Posted at

とある理由でWebアプリケーション版文字変換システム(Base64/URLのエンコード・デコード)が環境依存により利用できなくなると判明したため,スタンドアローンで利用できる,Windowsアプリケーション版文字変換システムを作成した.その際利用したメソッド等のメモを残しておく.

1.開発環境

Visual Studio2015のCommunityバージョンを用いてWindowsアプリケーションを作成した.

  Microsoft Visual Studio Community 2015
  Version 14.0.24720.00 Update 1
  Microsoft .NET Framework
  Version 4.6.01055

  インストールされているバージョン:Community

  Visual Basic 2015   00322-20000-00000-AA686
  Microsoft Visual Basic 2015

  Visual C# 2015   00322-20000-00000-AA686
  Microsoft Visual C# 2015

  Visual C++ 2015   00322-20000-00000-AA686
  Microsoft Visual C++ 2015

  Application Insights Tools for Visual Studio のパッケージ   4.2.60128.3
  Application Insights Tools for Visual Studio

  ASP.NET and Web Tools 2015.1 (Beta8)   14.1.11106.0
  ASP.NET and Web Tools 2015.1 (Beta8)

  ASP.NET Web Frameworks and Tools 2012.2   4.1.41102.0
  For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

  ASP.NET Web Frameworks and Tools 2013   5.2.30624.0
  For additional information, visit http://www.asp.net/

  Common Azure Tools   1.5
  Azure Mobile Services および Microsoft Azure Tools で使用する共通サービスを提供します。

  Microsoft Azure Mobile Services Tools   1.4
  Microsoft Azure Mobile Services Tools

  NuGet パッケージ マネージャー   3.3.0
  Visual Studio 内の NuGet パッケージ マネージャー。NuGet の詳細については、http://docs.nuget.org/ にアクセスしてください。

  PreEmptive Analytics Visualizer   1.2
  Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

  SQL Server Data Tools   14.0.50616.0
  Microsoft SQL Server Data Tools

  TypeScript   1.7.6.0
  TypeScript for Microsoft Visual Studio

2.キーポイント

システム全体のつくりとしては,Base64/URLエンコード・デコードおよび文字参照のエンコード・デコードに対応するアプリケーションを作成した.

2.1.Base64

Base64エンコード・デコードにおいて取り扱う文字コードは,「UTF-8」「Shift-JIS」「EUC-JP」「JIS(iso-2022-jp)」である.
ユーザに文字コードおよびエンコード・デコードを指定してもらい,テキストボックスに入力された文字を変換し,別のテキストボックスに
結果を表示する.

技術的に見てみると,文字列を指定された文字コードに従ってバイト型で変数に格納し,バイト型で格納されている文字列をbase64エンコード・デコードしている.

ソースコード

   // テキストボックス(base64_textbox_before)に入力された文字列を取得する
   string input_str = base64_textbox_before.Text;

   // 指定された文字コードでインスタンス化する
   Encoding enc = Encoding.GetEncoding(base64_radio_button);

   // 指定された文字コードに従い,バイト型で取り扱う
   byte[] convert_str = enc.GetBytes(input_str);

   // Base64エンコードし,文字列として結果を保存する(注:引数はコードページ値もしくはWebNameプロパティ値であること)
   string after_str = Convert.ToBase64String(convert_str);

   // デコードする場合,以下の方法でデコードする
   byte[] convert_str = Convert.FromBase64String(input_str);
   Encoding enc = Encoding.GetEncoding(base64_radio_button);
   string after_str = enc.GetString(convert_str);

   // 変換結果をもう一つのテキストボックス(base64_textbox_after)に表示する
   base64_textbox_after.Text = after_str;

2.2.URLエンコード

URLエンコード・デコードにおいて取り扱う文字コードは,「UTF-8」「Shift-JIS」「EUC-JP」である.
Base64機能と同じで,ユーザに文字コードおよびエンコード・デコードを指定してもらい,結果を表示させる.
技術的に見てみると,string型で取り扱うためBase64の時より1ステップ少なくなる.

ソースコード
   // テキストボックスに入力された文字列を取得
   string input_str = url_textbox_before.Text;

   // 指定された文字コードを取得
   System.Text.Encoding charcode = System.Text.Encoding.GetEncoding(url_radio_button);

   // 文字列を指定された文字コードに従い,URLエンコードする
   string after_str = System.Web.HttpUtility.UrlEncode(input_str,charcode);
   
   // デコードの場合,以下のメソッドを利用する
   string after_str = System.Web.HttpUtility.UrlDecode(tmp_str,charcode);

   // 変換結果をもう一つのテキストボックスに表示する
   url_textbox_after.Text = after_str;

2.3.文字参照

課題が残る機能ではあるが,デコードの場合,問題なく数値文字参照および文字実体参照の対応する文字列が表示されるが,エンコードの場合,文字実体参照の結果しか表示されない.原因は,使用しているメソッドが文字実体参照しか取り扱っていないため.そのうち機能追加する予定.

ソースコード
   // 文字実体参照にエンコードする
   string after_str = System.Web.HttpUtility.HtmlEncode(input_str);

   // 数値文字参照および文字実体参照をデコードする
   string after_str = System.Web.HttpUtility.HtmlDecode(input_str);

3.Appendix

3.1.「Ctrl+A」の有効化

テキストボックス内部においてデフォルトでは「Ctrl+A」による全選択が無効化されている.
以下を追加して,有効化する.

ソースコード
   // 以下を対応するForm.Designer.csに追記する   
   this.[textbox_name].KeyDown += new System.Windows.Forms.KeyEventHandler(this.[textbox_name]_KeyDown);

   // 以下を対応するForm.csに追記する
   private void [textbox_name]_KeyDown(object sender, KeyEventArgs e)
   {
     if (e.Control && e.KeyCode == Keys.A)
     {
       [textbox_name].SelectAll();
     }
   }

3.2.comboBoxの初期値設定

フォームデザインにおけるcomboBoxは,プロパティから初期値を設定する方法がよくわからない.
そこで,強制的に初期値を設定する.なお,文字列コレクションの上から順にindexが0,1,2,・・・というように扱われている.

ソースコード
   // 以下を対応するForm.Designer.csに追記する
   // 今回はindex=0(文字列コレクションの一番上の文字列)を初期値とする
   this.comboBox1.SelectedIndex = 0;
1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?