ConvertTool.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Curtain.Net.Sockets.PLC
  5. {
  6. /// <summary>
  7. /// 转换工具类
  8. /// </summary>
  9. public static class ConvertTool
  10. {
  11. #region Siemens S7
  12. /// <summary>
  13. /// Siemens S7 字节反转
  14. /// </summary>
  15. /// <param name="data"></param>
  16. /// <returns></returns>
  17. public static byte[] ReverseBy2(byte[] data)
  18. {
  19. int count = data.Length;
  20. if (count % 2 != 0)
  21. {
  22. count--;
  23. }
  24. byte[] rData = new byte[count];
  25. for (int i = 0, j = 1; i < count && j < count; i += 2, j += 2)
  26. {
  27. rData[i] = data[j];
  28. rData[j] = data[i];
  29. }
  30. return rData;
  31. }
  32. /// <summary>
  33. /// 16进制字符转byte[]
  34. /// </summary>
  35. /// <param name="hex"></param>
  36. /// <param name="length"></param>
  37. /// <returns></returns>
  38. public static byte[] HexStrToBytes(string hex, int length = 2)
  39. {
  40. if (string.IsNullOrWhiteSpace(hex))
  41. {
  42. return null;
  43. }
  44. if (hex.IndexOf(' ') > 0)
  45. {
  46. hex = hex.Replace(" ", "");
  47. }
  48. int i = 0;
  49. List<byte> bbs = new List<byte>();
  50. while (i < hex.Length)
  51. {
  52. bbs.Add(Convert.ToByte(hex.Substring(i, length), 16));
  53. i += length;
  54. }
  55. return bbs.ToArray();
  56. }
  57. /// <summary>
  58. /// byte[]转16进制字符
  59. /// </summary>
  60. /// <param name="bytes"></param>
  61. /// <param name="length"></param>
  62. /// <returns></returns>
  63. public static string BytesToHexStr(byte[] bytes, int length = 2)
  64. {
  65. if (bytes == null || bytes.Length == 0)
  66. {
  67. return null;
  68. }
  69. StringBuilder sb = new StringBuilder();
  70. foreach (byte item in bytes)
  71. {
  72. sb.Append(ByteToHexStr(item, length));
  73. }
  74. return sb.ToString();
  75. }
  76. /// <summary>
  77. /// byte转16进制字符
  78. /// </summary>
  79. /// <param name="item"></param>
  80. /// <param name="length"></param>
  81. /// <returns></returns>
  82. public static string ByteToHexStr(byte item, int length = 2)
  83. {
  84. return Convert.ToString(item, 16).ToUpper().PadLeft(length, '0');
  85. }
  86. /// <summary>
  87. /// bool[]转byte[]
  88. /// 8个bool合成一个byte
  89. /// Siemens S7
  90. /// </summary>
  91. /// <param name="data"></param>
  92. /// <returns></returns>
  93. public static byte[] BoolsToBytes(bool[] data)
  94. {
  95. if (data == null)
  96. {
  97. return null;
  98. }
  99. List<byte> bytes = new List<byte>();
  100. for (int i = 0; i < data.Length; i += 8)
  101. {
  102. byte item = 0;
  103. for (int j = 0; j < 8 && i+j< data.Length; j++)
  104. {
  105. if (data[i + j])
  106. {
  107. item += (byte)Math.Pow(2, j);
  108. }
  109. }
  110. bytes.Add(item);
  111. }
  112. return bytes.ToArray();
  113. }
  114. /// <summary>
  115. /// byte[]转bool[]
  116. /// 一个byte拆成8个bool
  117. /// Siemens S7
  118. /// </summary>
  119. /// <param name="data"></param>
  120. /// <param name="length"></param>
  121. /// <returns></returns>
  122. public static bool[] BytesToBools(byte[] data, int length)
  123. {
  124. if (data == null)
  125. {
  126. return null;
  127. }
  128. List<bool> bools = new List<bool>();
  129. foreach (byte item in data)
  130. {
  131. char[] cs = Convert.ToString(item, 2).ToCharArray();
  132. Array.Reverse(cs);
  133. int count = Math.Min(8, length);
  134. length -= 8;
  135. bool[] bs = new bool[count];
  136. for (int i = 0; i < count && i< cs.Length; i++)
  137. {
  138. bs[i] = (cs[i] == '1');
  139. }
  140. bools.AddRange(bs);
  141. }
  142. return bools.ToArray();
  143. }
  144. #endregion
  145. }
  146. }