• 硬件协议帮助类


      1         /// <summary>
      2         /// 十六进制转成字节
      3         /// </summary>
      4         /// <param name="aHex"></param>
      5         /// <returns></returns>
      6         public string HexToBytes(string aHex)
      7         {
      8             aHex = aHex.ToUpper();
      9             int len = aHex.Length;
     10             string ss = "";
     11             string sss = "";
     12             for (int i = 1; i <= len; i++)
     13             {
     14                 string s = "";
     15                 s = aHex.Substring(i - 1, 1);
     16                 switch (s)
     17                 {
     18                     case "0":
     19                         ss = "0000";
     20                         break;
     21                     case "1":
     22                         ss = "0001";
     23                         break;
     24                     case "2":
     25                         ss = "0010";
     26                         break;
     27                     case "3":
     28                         ss = "0011";
     29                         break;
     30                     case "4":
     31                         ss = "0100";
     32                         break;
     33                     case "5":
     34                         ss = "0101";
     35                         break;
     36                     case "6":
     37                         ss = "0110";
     38                         break;
     39                     case "7":
     40                         ss = "0111";
     41                         break;
     42                     case "8":
     43                         ss = "1000";
     44                         break;
     45                     case "9":
     46                         ss = "1001";
     47                         break;
     48                     case "A":
     49                         ss = "1010";
     50                         break;
     51                     case "B":
     52                         ss = "1011";
     53                         break;
     54                     case "C":
     55                         ss = "1100";
     56                         break;
     57                     case "D":
     58                         ss = "1101";
     59                         break;
     60                     case "E":
     61                         ss = "1110";
     62                         break;
     63                     case "F":
     64                         ss = "1111";
     65                         break;
     66                     default:
     67 
     68                         break;
     69 
     70                 }
     71                 sss += ss;
     72             }
     73             return sss;
     74         }
     75         /// <summary>
     76         /// 汉字转国标码
     77         /// </summary>
     78         /// <param name="hz"></param>
     79         /// <returns></returns>
     80         public string Hz2ASC(string hz)
     81         {
     82             string st = string.Empty;
     83             byte[] array = System.Text.Encoding.Default.GetBytes(hz);
     84             for (int i = 0; i < array.Length; i++)
     85             {
     86                 st += array[i].ToString("X2");
     87             }
     88             return st;
     89         }
     90         /// <summary>
     91         /// 国标码转汉字
     92         /// </summary>
     93         /// <param name="asc"></param>
     94         /// <returns></returns>
     95         public string ASC2Hz(string asc)
     96         {
     97             string st = string.Empty;
     98             byte[] array = new byte[asc.Length / 2];
     99             for (int i = 0; i < array.Length; i++)
    100                 array[i] = Convert.ToByte(asc.Substring(i * 2, 2).Trim(), 16);
    101             for (int i = 0; i < array.Length; i++)
    102             {
    103                 if (array[i] >= 161 && array[i] <= 247)
    104                 {
    105                     st = st + System.Text.Encoding.Default.GetString(array, i, 2);
    106                     i++;
    107                 }
    108                 else
    109                 {
    110                     st = st + System.Text.Encoding.Default.GetString(array, i, 1);
    111                 }
    112             }
    113             return st;
    114         }
    115         /// <summary>
    116         /// 从字符串头位置取指定长度的字符串,
    117         /// 并从源字符串删除取出的字符串
    118         /// </summary>
    119         /// <param name="str">源字符串</param>
    120         /// <param name="getLen">要取的长度</param>
    121         /// <returns>返回取出的字符串</returns>
    122         public string getString(ref string str, int getLen)
    123         {
    124             string backStr = "";
    125             try
    126             {
    127                 if ((!str.Equals("")) && (str.Length >= getLen))
    128                 {
    129                     backStr = str.Substring(0, getLen);
    130                     str = str.Substring(getLen);
    131                 }
    132             }
    133             catch { }
    134             return backStr;
    135         }
    136         /// <summary>
    137         /// 从字符串右边位置取指定长度的字符串,
    138         /// 并从源字符串删除取出的字符串
    139         /// </summary>
    140         /// <param name="str"></param>
    141         /// <param name="getLen"></param>
    142         /// <returns></returns>
    143         public string getStringR(ref string str, int getLen)
    144         {
    145             string backStr = "";
    146             try
    147             {
    148                 if ((!str.Equals("")) && (str.Length >= getLen))
    149                 {
    150                     backStr = str.Substring(str.Length - getLen);
    151                     str = str.Substring(0, str.Length - getLen);
    152                 }
    153             }
    154             catch { }
    155             return backStr;
    156         }
    157 
    158         /// <summary>
    159         /// 十六进制转换成十进制
    160         /// </summary>
    161         /// <param name="hexStr">十六进制字符串</param>
    162         /// <returns>十进制数</returns>
    163         public int Hex2Int(string hexStr)
    164         {
    165             int intValue;
    166             try
    167             {
    168                 intValue = int.Parse(hexStr, System.Globalization.NumberStyles.HexNumber);
    169             }
    170             catch
    171             {
    172                 intValue = 0;
    173             }
    174             return intValue;
    175         }
    176         /// <summary>
    177         /// 字符串(必须是BCD码串)转十六进制(返回结果高端在前)
    178         /// </summary>
    179         /// <param name="str">字串</param>
    180         /// <param name="len">转换后长度,为字符长度</param>
    181         /// <returns></returns>
    182         public string Str2Hex(string str, int len)
    183         {
    184             string hex = "";
    185             try
    186             {
    187                 int strInt = Convert.ToInt32(str);
    188                 hex = strInt.ToString("X" + len);
    189             }
    190             catch
    191             {
    192                 for (int i = 0; i < len; i++) hex += "0";
    193             }
    194             return hex;
    195         }
    196         /// <summary>
    197         /// 字符串(必须是BCD码串)转十六进制(返回结果低端在前)
    198         /// </summary>
    199         /// <param name="str">字串</param>
    200         /// <param name="len">转换后长度,为字符长度</param>
    201         /// <returns></returns>
    202         public string Str2HexLow(string str, int len)
    203         {
    204             string hex = "";
    205             try
    206             {
    207                 int strInt = Convert.ToInt32(str);
    208                 hex = strInt.ToString("X" + len);
    209                 //颠倒一下
    210                 string temp="";
    211                 for (int i = 0; i < hex.Length / 2; i++)
    212                 {
    213                     temp += hex.Substring(hex.Length - (2 * i + 2), 2);
    214                 }
    215                 hex = temp;
    216             }
    217             catch
    218             {
    219                 for (int i = 0; i < len; i++) hex += "0";
    220             }
    221             return hex;
    222         }
    223         /// <summary>
    224         /// string类型的ASCII字符转换为string类型的十六进制(如“AB426EPC11”-> “41423432364550433131”)
    225         /// 注意是大写的
    226         /// </summary>
    227         /// <param name="StringASCII">要转换的ASCII字符</param>
    228         /// <returns></returns>
    229         public string ASCII2string(string StringASCII)
    230         {
    231             string Data = StringASCII;
    232             int i;
    233             int intData;
    234             string hexData;
    235             string changeData = "";
    236 
    237             for (i = 0; i < Data.Length; i++)
    238             {
    239                 string stringData = Data.Substring(i, 1).ToUpper();
    240                 char charData = Convert.ToChar(stringData);
    241                 intData = Convert.ToInt32(charData);
    242                 hexData = intData.ToString("X");
    243                 changeData += hexData;
    244             }
    245             return changeData;
    246         }
    247         /// <summary>
    248         /// string类型的ASCII字符转换为string类型的十六进制(如“AB426EPC11”-> “41423432364550433131”)
    249         /// 注意是小写的
    250         /// </summary>
    251         /// <param name="StringASCII">要转换的ASCII字符</param>
    252         /// <returns></returns>
    253         public string ASCII2stringLower(string StringASCII)
    254         {
    255             string Data = StringASCII;
    256             int i;
    257             int intData;
    258             string hexData;
    259             string changeData = "";
    260 
    261             for (i = 0; i < Data.Length; i++)
    262             {
    263                 string stringData = Data.Substring(i, 1).ToLower();
    264                 char charData = Convert.ToChar(stringData);
    265                 intData = Convert.ToInt32(charData);
    266                 hexData = intData.ToString("X");
    267                 changeData += hexData;
    268             }
    269             return changeData;
    270         }
    271         /// <summary>
    272         /// string类型的ASCII字符转换为string类型的十六进制(如“AB426EPC11”-> “41423432364550433131”)
    273         /// 注意是区分大小写的
    274         /// </summary>
    275         /// <param name="StringASCII">要转换的ASCII字符</param>
    276         /// <returns></returns>
    277         public string ASCII2stringCase(string StringASCII)
    278         {
    279             string Data = StringASCII;
    280             int i;
    281             int intData;
    282             string hexData;
    283             string changeData = "";
    284 
    285             for (i = 0; i < Data.Length; i++)
    286             {
    287                 string stringData = Data.Substring(i, 1);
    288                 char charData = Convert.ToChar(stringData);
    289                 intData = Convert.ToInt32(charData);
    290                 hexData = intData.ToString("X");
    291                 changeData += hexData;
    292             }
    293             return changeData;
    294         }
    295         /// <summary>
    296         /// string类型的HEX转换为string类型的ASCII字符(如“41423432364550433131”-> “AB426EPC11”)
    297         /// </summary>
    298         /// <param name="stringhex">要转换的string类型的HEX</param>
    299         /// <returns>ASCII字符</returns>
    300         public string string2ASCII(string stringhex)
    301         {
    302             string changeData = "";
    303             try
    304             {
    305                 string hexData = stringhex;
    306                 int i;
    307                 char stringData;
    308                 int intData;
    309 
    310 
    311                 for (i = 0; i < hexData.Length; i = i + 2)
    312                 {
    313                     string tempData = hexData.Substring(i, 2);
    314                     intData = Convert.ToInt32(tempData, 16);
    315                     stringData = (char)intData;
    316                     changeData += stringData.ToString();
    317                 }
    318                 return changeData;
    319             }
    320             catch (Exception e)
    321             {
    322                 return changeData = e.Message;
    323             }
    324         }
    325         /// <summary>
    326         /// BCD转成固定长度的String
    327         /// </summary>
    328         /// <param name="str"></param>
    329         /// <param name="len"></param>
    330         /// <returns></returns>
    331         public string BCD2String(string str, int len)
    332         {
    333             int ll = str.Length;
    334             for (int i = 0; i < len - ll; i++)
    335             {
    336                 str = "0" + str;
    337             }
    338             return str.Substring(0, len);
    339         }
  • 相关阅读:
    JAVA 单例模式
    CodeForces Round #563 Div.2
    拓扑排序 JAVA
    初识 Dubbo
    CodeForces Round #567 Div.2
    Educational Codeforces Round 65 (Rated for Div. 2)
    最短路径问题
    C++使用fixed和precision控制小数和有效位数的输出以及setw()设置输出宽度
    poj3684(弹性碰撞模型)
    集合的整数表示
  • 原文地址:https://www.cnblogs.com/Archosaur/p/AgreementClassHelper.html
Copyright © 2020-2023  润新知