Quantcast
Channel: 吟遊詩人の戯言
Viewing all articles
Browse latest Browse all 17795

【C#】S-JIS化した文字列の指定位置の文字種を得る関数

$
0
0

めんどくせぇ~~(苦笑
UniCodeの時代やじぃ~.今どき,S-JISの文字種チェックが必要になるとはのぉ

VB6とか,VC++4とか,DELPHI5時代のS-JISチェック,すでに捨てたじぃ
いや,どこかにあるじゃろけど,アーカイブを解凍するのがメンドクサ(爆

某システム向けの,特殊プリンタに流しこむ文字コードが,S-JISで
文字列を切り貼りした時,中途半端に文字列末に全角1バイト目が残ったりでもしたら
機械が誤動作するのは,この業界,周知の事実じゃろしのぉ?

ってことで,C#の文字列(UniCode)を急遽書き起こしてみた
全くチェックしてないじぃ(いや,今から,チェックするけど)

ってことで,バグってたらスマンこってす

  1. //------------------------
  2. //sjisの文字種チェック
  3. //return -1: 文字位置不正
  4. //       -2: byte配列不正
  5. //       -3: それ以外不正
  6. //        1: 制御文字
  7. //        2: 一般半角
  8. //        3: カナ半角
  9. //       11: 全角1バイト目
  10. //       12: 全角2バイト目
  11. //       99: それ以外の文字
  12. //
  13. //------------------------
  14. public static int is_SjisCharType(byte[] buf, int checkloc)
  15. {
  16.     int result = -3;
  17.  
  18.     if (buf.Length == 0)
  19.     {
  20.         return -2;
  21.     }
  22.  
  23.     if ((checkloc < 0) || (checkloc >= buf.Length))
  24.     {
  25.         return -1;
  26.     }
  27.  
  28.     bool zenkakufl = false;
  29.     for(int i = 0; i < buf.Length; i++)
  30.     {
  31.         byte c = buf[i];
  32.  
  33.         if (zenkakufl == false)             //通常モード
  34.         {
  35.             if ((c >= 0) && (c < 0x20))     //制御コード
  36.             {
  37.                 result = 1;
  38.             }
  39.             else if ((c >= 0x20) && (c <= 0x7f))            //一般半角
  40.             {
  41.                 result = 2;
  42.             }
  43.             else if (((c >= 0x81) && (c <= 0x9f)) || ((c >= 0xe0) && (c <= 0xfc)))      //全角1バイト目
  44.             {
  45.                 result = 11;
  46.                 zenkakufl = true;
  47.             }
  48.             else if ((c >= 0xa0) && (c <= 0xdf))        //カナ半角
  49.             {
  50.                 result = 3;
  51.             }
  52.             else                //それ以外の文字
  53.             {
  54.                 result = 99;
  55.             }
  56.         }
  57.         else                            //全角1バイト目モード
  58.         {
  59.             if ((c >= 0x40) && (c <= 0xfc))         //全角2バイト目
  60.             {
  61.                 result = 12;
  62.             }
  63.             else                           //それ以外の文字種
  64.             {
  65.                 result = 99;
  66.             }
  67.             zenkakufl = false;
  68.         }
  69.  
  70.  
  71.         if (i == checkloc)              //指定位置が来たら抜ける
  72.         {
  73.             break;
  74.         }
  75.     }
  76.  
  77.     return result;
  78.  
  79. }
  80.  
  81.  
  82. private void btn_Click(object sender, EventArgs e)
  83. {
  84.     string st = "aイウ絵男";
  85.  
  86.     byte[] tmpByteAry = System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(st);   //文字列をS-JISでエンコードし,Byte配列に格納
  87.  
  88.     int i = classCommonInfo.is_SjisCharType(tmpByteAry, 1);        //指定位置の文字種を取得
  89.  
  90.     MessageBox.Show(i.ToString(), "確認",
  91.                         MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  92.  
  93. }

Viewing all articles
Browse latest Browse all 17795

Trending Articles