迷你5207专属论坛

注册

 

发新话题 回复该主题

[DotNet] C#实现byte,hex,str转换的类 [复制链接]

发表者
  1. class CardFun
  2.     {
  3.         #region HEX,BIN,STR 互转
  4.         //以下4方法 用于数据间的互转,并可通过互相调用,达到跳跃转换的目的
  5.         //Bin 转 BCD 的显示(str)
  6.         public static string B_to_BCD(byte[] data)
  7.         {
  8.             //和转hex一样,但是多一个check
  9.             string backstr = "";
  10.             string str = "";
  11.             for (int i = 0; i < data.Length; i++)
  12.             {
  13.                 str = &quot;00&quot; + data[i].ToString(&quot;X&quot;);
  14.                 str = str.Substring(str.Length - 2, 2);
  15.                 if (str.ToUpper().IndexOf(&quot;A&quot;) != -1 || str.ToUpper().IndexOf(&quot;B&quot;) != -1 || str.ToUpper().IndexOf(&quot;C&quot;) != -1 || str.ToUpper().IndexOf(&quot;D&quot;) != -1 || str.ToUpper().IndexOf(&quot;E&quot;) != -1 || str.ToUpper().IndexOf(&quot;F&quot;) != -1)
  16.                 {
  17.                     return backstr = &quot;convert BCD err&quot;;
  18.                 }
  19.                 backstr += str;
  20.             }
  21.             return backstr;
  22.         }
  23.         //Bin 转 Hex 的显示(str)
  24.         public static string B_to_Hstr(byte[] data)
  25.         {
  26.             string backstr = &quot;&quot;;
  27.             string str = &quot;&quot;;
  28.             for (int i = 0; i < data.Length; i++)
  29.             {
  30.                 str = &quot;00&quot; + data[i].ToString(&quot;X&quot;);
  31.                 str = str.Substring(str.Length - 2, 2);
  32.                 backstr += str;
  33.             }
  34.             return backstr;
  35.         }
  36.         //Bin 转 Bin 的显示(str,逗号分隔)
  37.         public static string B_to_Bstr(byte[] data)
  38.         {
  39.             string backstr = &quot;&quot;;
  40.             string dash = &quot;&quot;;
  41.             for (int i = 0; i < data.Length; i++)
  42.             {
  43.                 backstr += dash + data[i];
  44.                 dash = &quot;,&quot;;
  45.             }
  46.             return backstr;
  47.         }
  48.         //Hex(str) 转 Bin
  49.         public static byte[] strH_to_B(string hex)
  50.         {
  51.             hex = hex.Trim();
  52.             byte[] bt;
  53.             int hexlen;
  54.             if (hex.Length % 2 != 0)
  55.             {
  56.                 hex = &quot;0&quot; + hex;
  57.             }
  58.             hexlen = hex.Length / 2;
  59.             bt = new byte[hexlen];
  60.             for (int i = 0; i < hexlen; i++)
  61.             {
  62.                 bt[i] = byte.Parse(hex.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
  63.             }
  64.             return bt;
  65.         }

  66.         //以下4方法 用于读卡时显示数据的转换
  67.         //Bin 转BCD显示,此卡特别的,认为存在F的BCD不为错,(F仅仅为填充)
  68.         public static string getBCD(byte[] data)
  69.         {
  70.             string str = &quot;&quot;;
  71.             //认为本程序中BCD不会出现F错误使用,F就是填充
  72.             str = B_to_Hstr(data).Replace(&quot;F&quot;, &quot;&quot;);
  73.             if (str.ToUpper().IndexOf(&quot;A&quot;) != -1 || str.ToUpper().IndexOf(&quot;B&quot;) != -1 || str.ToUpper().IndexOf(&quot;C&quot;) != -1 || str.ToUpper().IndexOf(&quot;D&quot;) != -1 || str.ToUpper().IndexOf(&quot;E&quot;) != -1 || str.ToUpper().IndexOf(&quot;F&quot;) != -1)
  74.             {
  75.                 str = &quot;convert BCD err&quot;;
  76.             }
  77.             //str = B_to_BCD(strH_to_B(B_to_Hstr(data).Replace(&quot;F&quot;,&quot;&quot;)));//better check,but low effect
  78.             return str;
  79.         }
  80.         //Bin 转Bin的显示
  81.         public static string getBin(byte[] data)
  82.         {
  83.             string str = &quot;&quot;;
  84.             str = B_to_Bstr(data);
  85.             return str;
  86.         }
  87.         //Bin 转Hex的显示
  88.         public static string getHex(byte[] data)
  89.         {
  90.             string str = &quot;&quot;;
  91.             str = B_to_Hstr(data);
  92.             return str;
  93.         }
  94.         //Bin 转Str的显示
  95.         public static string getStr(byte[] data)
  96.         {
  97.             string str = &quot;&quot;;
  98.             Encoding gb2312 = Encoding.GetEncoding(&quot;gb2312&quot;);
  99.             str = gb2312.GetString(data);          
  100.             return str;
  101.         }
  102.         #endregion
  103.         //获得卡中数据存放位置
  104.         public static int getaddr(int sector, int block, int loc)
  105.         {
  106.             return (sector * 4 + block) * 16 + loc;
  107.         }
  108.     }
复制代码
本主题由 管理员 5207 于 2007-12-2 11:15:36 执行 主题分类 操作
分享 转发
相信与不相信都是矛盾的.  5207宣!欢迎您来到点滴论坛
TOP
发新话题 回复该主题