四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2267|回复: 0
打印 上一主题 下一主题

低层通讯常用的字符及字符串转换函数

[复制链接]
  • TA的每日心情
    开心
    2018-11-9 08:52
  • 241

    主题

    691

    帖子

    7652

    积分

    论坛元老

    Rank: 8Rank: 8

    积分
    7652
    跳转到指定楼层
    楼主
    发表于 2016-11-10 15:06:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1.     public static string byteStr(ushort val)
    2.     {
    3.         byte num = (byte) ((val >> 8) & 0xff);
    4.         byte num2 = (byte) (val & 0xff);
    5.         return string.Format("{0:X2} {1:X2}", num, num2);
    6.     }

    7.     public static ulong HexToInt(string hexString)
    8.     {
    9.         string str = "0123456789abcdef";
    10.         string str2 = hexString.ToLower();
    11.         if ((str2.Length > 2) && str2.Substring(0, 2).Equals("0x"))
    12.         {
    13.             str2 = str2.Substring(2, str2.Length - 2);
    14.         }
    15.         string str3 = "";
    16.         int length = str2.Length;
    17.         for (int i = length - 1; i >= 0; i--)
    18.         {
    19.             str3 = str3 + str2[i];
    20.         }
    21.         ulong num3 = 0L;
    22.         ulong num4 = 1L;
    23.         for (int j = 0; j < length; j++)
    24.         {
    25.             uint index = (uint) str.IndexOf(str3[j]);
    26.             num3 += (j == 0) ? ((ulong) index) : (num4 * index);
    27.             num4 *= (ulong) 0x10L;
    28.         }
    29.         return num3;
    30.     }

    31.     public static bool ParseHexData(ArrayList rawList, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
    32.     {
    33.         string str;
    34.         for (int i = rawList.Count - 1; i >= 0; i--)
    35.         {
    36.             str = (string) rawList[i];
    37.             if ((str.Length > 0) && (((int) HexToInt(str.Substring(7, 2))) != 0))
    38.             {
    39.                 rawList.Remove(rawList[i]);
    40.             }
    41.         }
    42.         FwLen = 0;
    43.         FwOff = _MAX_FW_SIZE;
    44.         for (int j = 0; j < _MAX_FW_SIZE; j++)
    45.         {
    46.             FwBuf[j] = 0xff;
    47.         }
    48.         for (int k = 0; k < rawList.Count; k++)
    49.         {
    50.             str = (string) rawList[k];
    51.             int index = str.IndexOf("//");
    52.             if (index > -1)
    53.             {
    54.                 str = str.Substring(0, index - 1);
    55.             }
    56.             if (str.Length > 0)
    57.             {
    58.                 ushort num5 = (ushort) HexToInt(str.Substring(3, 4));
    59.                 if (num5 >= _MAX_FW_SIZE)
    60.                 {
    61.                     return false;
    62.                 }
    63.                 if (num5 < FwOff)
    64.                 {
    65.                     FwOff = num5;
    66.                 }
    67.                 index = ((int) HexToInt(str.Substring(1, 2))) * 2;
    68.                 string str4 = str.Substring(9, index);
    69.                 int num6 = index / 2;
    70.                 int num7 = 0;
    71.                 while (num7 < num6)
    72.                 {
    73.                     FwBuf[num5] = (byte) HexToInt(str4.Substring(num7 * 2, 2));
    74.                     num7++;
    75.                     num5 = (ushort) (num5 + 1);
    76.                 }
    77.                 if (num5 > FwLen)
    78.                 {
    79.                     FwLen = num5;
    80.                 }
    81.             }
    82.         }
    83.         return true;
    84.     }

    85.     public static bool ParseHexFile(string fName, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
    86.     {
    87.         if (File.Exists(fName))
    88.         {
    89.             ArrayList rawList = new ArrayList();
    90.             StreamReader reader = new StreamReader(fName);
    91.             if (reader != null)
    92.             {
    93.                 string str;
    94.                 while ((str = reader.ReadLine()) != null)
    95.                 {
    96.                     rawList.Add(str);
    97.                 }
    98.                 reader.Close();
    99.                 return ParseHexData(rawList, FwBuf, ref FwLen, ref FwOff);
    100.             }
    101.         }
    102.         return false;
    103.     }

    104.     public static unsafe void ParseIICData(byte[] fData, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
    105.     {
    106.         ushort xStart = 8;
    107.         FwLen = 0;
    108.         FwOff = _MAX_FW_SIZE;
    109.         for (int i = 0; i < _MAX_FW_SIZE; i++)
    110.         {
    111.             FwBuf[i] = 0xff;
    112.         }
    113.         fixed (byte* numRef = fData)
    114.         {
    115.             ushort* numPtr;
    116.             do
    117.             {
    118.                 ReverseBytes(fData, xStart, 2);
    119.                 ReverseBytes(fData, xStart + 2, 2);
    120.                 numPtr = (ushort*) (numRef + xStart);
    121.                 ushort* numPtr2 = (ushort*) ((numRef + xStart) + 2);
    122.                 if (numPtr[0] != 0x8001)
    123.                 {
    124.                     Array.Copy(fData, xStart + 4, FwBuf, (int) numPtr2[0], (int) numPtr[0]);
    125.                     ushort num3 = (ushort) (numPtr2[0] + numPtr[0]);
    126.                     if (num3 > FwLen)
    127.                     {
    128.                         FwLen = num3;
    129.                     }
    130.                     if (numPtr2[0] < FwOff)
    131.                     {
    132.                         FwOff = numPtr2[0];
    133.                     }
    134.                 }
    135.                 xStart = (ushort) (xStart + ((ushort) (numPtr[0] + 4)));
    136.             }
    137.             while ((numPtr[0] != 0x8001) && (xStart < fData.Length));
    138.         }
    139.     }

    140.     public static bool ParseIICFile(string fName, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
    141.     {
    142.         if (!File.Exists(fName))
    143.         {
    144.             return false;
    145.         }
    146.         Stream stream = new FileStream(fName, FileMode.Open, FileAccess.Read);
    147.         if (stream == null)
    148.         {
    149.             return false;
    150.         }
    151.         int length = (int) stream.Length;
    152.         byte[] buffer = new byte[length];
    153.         stream.Read(buffer, 0, length);
    154.         stream.Close();
    155.         if (length > _MAX_FW_SIZE)
    156.         {
    157.             return false;
    158.         }
    159.         ParseIICData(buffer, FwBuf, ref FwLen, ref FwOff);
    160.         return true;
    161.     }

    162.     public static unsafe int ReverseBytes(byte* dta, int bytes)
    163.     {
    164.         if (bytes < 0)
    165.         {
    166.             return 0;
    167.         }
    168.         byte[] buffer = new byte[bytes];
    169.         for (byte i = 0; i < bytes; i = (byte) (i + 1))
    170.         {
    171.             buffer[i] = *(((dta + bytes) - i) - 1);
    172.         }
    173.         for (byte j = 0; j < bytes; j = (byte) (j + 1))
    174.         {
    175.             dta[j] = buffer[j];
    176.         }
    177.         return ((bytes > 2) ? BitConverter.ToInt32(buffer, 0) : BitConverter.ToInt16(buffer, 0));
    178.     }

    179.     public static int ReverseBytes(byte[] dta, int xStart, int bytes)
    180.     {
    181.         if (bytes < 0)
    182.         {
    183.             return 0;
    184.         }
    185.         byte[] buffer = new byte[bytes];
    186.         for (byte i = 0; i < bytes; i = (byte) (i + 1))
    187.         {
    188.             buffer[i] = dta[((xStart + bytes) - i) - 1];
    189.         }
    190.         for (byte j = 0; j < bytes; j = (byte) (j + 1))
    191.         {
    192.             dta[xStart + j] = buffer[j];
    193.         }
    194.         return ((bytes > 2) ? BitConverter.ToInt32(buffer, 0) : BitConverter.ToInt16(buffer, 0));
    195.     }
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-2 21:36 , Processed in 1.062500 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表