ConvertTool.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /// 16进制字符转byte[]
  14. /// </summary>
  15. /// <param name="hex"></param>
  16. /// <param name="length"></param>
  17. /// <returns></returns>
  18. public static byte[] HexStrToBytes(string hex, int length = 2)
  19. {
  20. if (string.IsNullOrWhiteSpace(hex))
  21. {
  22. return null;
  23. }
  24. if (hex.IndexOf(' ') > 0)
  25. {
  26. hex = hex.Replace(" ", "");
  27. }
  28. int i = 0;
  29. List<byte> bbs = new List<byte>();
  30. while (i < hex.Length)
  31. {
  32. bbs.Add(Convert.ToByte(hex.Substring(i, length), 16));
  33. i += length;
  34. }
  35. return bbs.ToArray();
  36. }
  37. /// <summary>
  38. /// byte[]转16进制字符
  39. /// </summary>
  40. /// <param name="bytes"></param>
  41. /// <param name="length"></param>
  42. /// <returns></returns>
  43. public static string BytesToHexStr(byte[] bytes, int length = 2)
  44. {
  45. if (bytes == null || bytes.Length == 0)
  46. {
  47. return null;
  48. }
  49. StringBuilder sb = new StringBuilder();
  50. foreach (byte item in bytes)
  51. {
  52. sb.Append(ByteToHexStr(item, length));
  53. }
  54. return sb.ToString();
  55. }
  56. /// <summary>
  57. /// byte转16进制字符
  58. /// </summary>
  59. /// <param name="item"></param>
  60. /// <param name="length"></param>
  61. /// <returns></returns>
  62. public static string ByteToHexStr(byte item, int length = 2)
  63. {
  64. return Convert.ToString(item, 16).ToUpper().PadLeft(length, '0');
  65. }
  66. /// <summary>
  67. /// bool[]转byte[]
  68. /// 8个bool合成一个byte
  69. /// Siemens S7
  70. /// </summary>
  71. /// <param name="data"></param>
  72. /// <returns></returns>
  73. public static byte[] BoolsToBytes(bool[] data)
  74. {
  75. if (data == null)
  76. {
  77. return null;
  78. }
  79. List<byte> bytes = new List<byte>();
  80. for (int i = 0; i < data.Length; i += 8)
  81. {
  82. byte item = 0;
  83. for (int j = 0; j < 8 && i+j< data.Length; j++)
  84. {
  85. if (data[i + j])
  86. {
  87. item += (byte)Math.Pow(2, j);
  88. }
  89. }
  90. bytes.Add(item);
  91. }
  92. return bytes.ToArray();
  93. }
  94. /// <summary>
  95. /// byte[]转bool[]
  96. /// 一个byte拆成8个bool
  97. /// Siemens S7
  98. /// </summary>
  99. /// <param name="data"></param>
  100. /// <param name="length"></param>
  101. /// <returns></returns>
  102. public static bool[] BytesToBools(byte[] data, int length)
  103. {
  104. if (data == null)
  105. {
  106. return null;
  107. }
  108. List<bool> bools = new List<bool>();
  109. foreach (byte item in data)
  110. {
  111. char[] cs = Convert.ToString(item, 2).ToCharArray();
  112. Array.Reverse(cs);
  113. int count = Math.Min(8, length);
  114. length -= 8;
  115. bool[] bs = new bool[count];
  116. for (int i = 0; i < count && i< cs.Length; i++)
  117. {
  118. bs[i] = (cs[i] == '1');
  119. }
  120. bools.AddRange(bs);
  121. }
  122. return bools.ToArray();
  123. }
  124. #endregion
  125. }
  126. }