终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

206 lines
6.8 KiB

  1. using Newtonsoft.Json.Linq;
  2. using S7.Net.Types;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPASmartClient.S7Net
  10. {
  11. public static class Conversion
  12. {
  13. /// <summary>
  14. /// 将二进制字符串转换为 Int32 值
  15. /// </summary>
  16. /// <param name="txt"></param>
  17. /// <returns></returns>
  18. public static int BinStringToInt32(this string txt)
  19. {
  20. int num = 0;
  21. for (int i = 0; i < txt.Length; i++)
  22. {
  23. num = (num << 1) | ((txt[i] == '1') ? 1 : 0);
  24. }
  25. return num;
  26. }
  27. /// <summary>
  28. /// 将二进制字符串转换为字节。 可以返回空值。
  29. /// </summary>
  30. /// <param name="txt"></param>
  31. /// <returns></returns>
  32. public static byte? BinStringToByte(this string txt)
  33. {
  34. if (txt.Length == 8)
  35. {
  36. return (byte)txt.BinStringToInt32();
  37. }
  38. return null;
  39. }
  40. /// <summary>
  41. /// 将值转换为二进制字符串
  42. /// </summary>
  43. /// <param name="value"></param>
  44. /// <returns></returns>
  45. public static string ValToBinString(this object value)
  46. {
  47. int num = 0;
  48. int num2 = 0;
  49. int num3 = 0;
  50. string text = "";
  51. long num4 = 0L;
  52. try
  53. {
  54. if (value.GetType().Name.IndexOf("[]") < 0)
  55. {
  56. switch (value.GetType().Name)
  57. {
  58. case "Byte":
  59. num3 = 7;
  60. num4 = (byte)value;
  61. break;
  62. case "Int16":
  63. num3 = 15;
  64. num4 = (short)value;
  65. break;
  66. case "Int32":
  67. num3 = 31;
  68. num4 = (int)value;
  69. break;
  70. case "Int64":
  71. num3 = 63;
  72. num4 = (long)value;
  73. break;
  74. default:
  75. throw new Exception();
  76. }
  77. for (num = num3; num >= 0; num += -1)
  78. {
  79. text = (((num4 & (long)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  80. }
  81. }
  82. else
  83. {
  84. switch (value.GetType().Name)
  85. {
  86. case "Byte[]":
  87. {
  88. num3 = 7;
  89. byte[] array4 = (byte[])value;
  90. for (num2 = 0; num2 <= array4.Length - 1; num2++)
  91. {
  92. for (num = num3; num >= 0; num += -1)
  93. {
  94. text = (((array4[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  95. }
  96. }
  97. break;
  98. }
  99. case "Int16[]":
  100. {
  101. num3 = 15;
  102. short[] array2 = (short[])value;
  103. for (num2 = 0; num2 <= array2.Length - 1; num2++)
  104. {
  105. for (num = num3; num >= 0; num += -1)
  106. {
  107. text = (((array2[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  108. }
  109. }
  110. break;
  111. }
  112. case "Int32[]":
  113. {
  114. num3 = 31;
  115. int[] array3 = (int[])value;
  116. for (num2 = 0; num2 <= array3.Length - 1; num2++)
  117. {
  118. for (num = num3; num >= 0; num += -1)
  119. {
  120. text = (((array3[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  121. }
  122. }
  123. break;
  124. }
  125. case "Int64[]":
  126. {
  127. num3 = 63;
  128. byte[] array = (byte[])value;
  129. for (num2 = 0; num2 <= array.Length - 1; num2++)
  130. {
  131. for (num = num3; num >= 0; num += -1)
  132. {
  133. text = (((array[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  134. }
  135. }
  136. break;
  137. }
  138. default:
  139. throw new Exception();
  140. }
  141. }
  142. return text;
  143. }
  144. catch
  145. {
  146. return "";
  147. }
  148. }
  149. /// <summary>
  150. /// 获取字节指定位的值
  151. /// </summary>
  152. /// <param name="data"></param>
  153. /// <param name="bitPosition"></param>
  154. /// <returns></returns>
  155. public static bool SelectBit(this byte data, int bitPosition)
  156. {
  157. int num = 1 << bitPosition;
  158. return (data & num) != 0;
  159. }
  160. public static short ConvertToShort(this ushort input)
  161. {
  162. return short.Parse(input.ToString("X"), NumberStyles.HexNumber);
  163. }
  164. public static ushort ConvertToUshort(this short input)
  165. {
  166. return ushort.Parse(input.ToString("X"), NumberStyles.HexNumber);
  167. }
  168. public static int ConvertToInt(this uint input)
  169. {
  170. return int.Parse(input.ToString("X"), NumberStyles.HexNumber);
  171. }
  172. public static uint ConvertToUInt(this int input)
  173. {
  174. return uint.Parse(input.ToString("X"), NumberStyles.HexNumber);
  175. }
  176. public static uint ConvertToUInt(this float input)
  177. {
  178. return DWord.FromByteArray(Real.ToByteArray(input));
  179. }
  180. public static float ConvertToFloat(this uint input)
  181. {
  182. return Real.FromByteArray(DWord.ToByteArray(input));
  183. }
  184. }
  185. }