SoftBasic.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. namespace TFT_MelsecMcNet
  10. {
  11. /// <summary>
  12. /// 一个软件基础类,提供常用的一些静态方法 ->
  13. /// A software-based class that provides some common static methods
  14. /// </summary>
  15. public class SoftBasic
  16. {
  17. #region MD5 Calculate
  18. /// <summary>
  19. /// 获取文件的md5码 -> Get the MD5 code of the file
  20. /// </summary>
  21. /// <param name="filePath">文件的路径,既可以是完整的路径,也可以是相对的路径 -> The path to the file</param>
  22. /// <returns>Md5字符串</returns>
  23. public static string CalculateFileMD5(string filePath)
  24. {
  25. string str_md5 = string.Empty;
  26. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  27. {
  28. str_md5 = CalculateStreamMD5(fs);
  29. }
  30. return str_md5;
  31. }
  32. /// <summary>
  33. /// 获取数据流的md5码 -> Get the MD5 code for the data stream
  34. /// </summary>
  35. /// <param name="stream">数据流,可以是内存流,也可以是文件流</param>
  36. /// <returns>Md5字符串</returns>
  37. public static string CalculateStreamMD5(Stream stream)
  38. {
  39. byte[] bytes_md5 = null;
  40. using (MD5 md5 = new MD5CryptoServiceProvider())
  41. {
  42. bytes_md5 = md5.ComputeHash(stream);
  43. }
  44. return BitConverter.ToString(bytes_md5).Replace("-", "");
  45. }
  46. /// <summary>
  47. /// 获取文本字符串信息的Md5码,编码为UTF8
  48. /// </summary>
  49. /// <param name="data">文本数据信息</param>
  50. /// <returns>Md5字符串</returns>
  51. public static string CalculateStreamMD5(string data)
  52. {
  53. return CalculateStreamMD5(data, Encoding.UTF8);
  54. }
  55. /// <summary>
  56. /// 获取文本字符串信息的Md5码,使用指定的编码
  57. /// </summary>
  58. /// <param name="data">文本数据信息</param>
  59. /// <param name="encode">编码信息</param>
  60. /// <returns>Md5字符串</returns>
  61. public static string CalculateStreamMD5(string data, Encoding encode)
  62. {
  63. string str_md5 = string.Empty;
  64. using (MD5 md5 = new MD5CryptoServiceProvider())
  65. {
  66. byte[] bytes_md5 = md5.ComputeHash(encode.GetBytes(data));
  67. str_md5 = BitConverter.ToString(bytes_md5).Replace("-", "");
  68. }
  69. return str_md5;
  70. }
  71. #if !NETSTANDARD2_0
  72. /// <summary>
  73. /// 获取内存图片的md5码 -> Get the MD5 code of the memory picture
  74. /// </summary>
  75. /// <param name="bitmap">内存图片</param>
  76. /// <returns>Md5字符串</returns>
  77. public static string CalculateStreamMD5(Bitmap bitmap)
  78. {
  79. MemoryStream ms = new MemoryStream();
  80. bitmap.Save(ms, bitmap.RawFormat);
  81. MD5 md5 = new MD5CryptoServiceProvider();
  82. byte[] bytes_md5 = md5.ComputeHash(ms);
  83. ms.Dispose();
  84. return BitConverter.ToString(bytes_md5).Replace("-", "");
  85. }
  86. #endif
  87. #endregion
  88. #region DataSize Format
  89. /// <summary>
  90. /// 从一个字节大小返回带单位的描述
  91. /// </summary>
  92. /// <param name="size">实际的大小值</param>
  93. /// <returns>最终的字符串值</returns>
  94. public static string GetSizeDescription(long size)
  95. {
  96. if (size < 1000)
  97. {
  98. return size + " B";
  99. }
  100. else if (size < 1000 * 1000)
  101. {
  102. float data = (float)size / 1024;
  103. return data.ToString("F2") + " Kb";
  104. }
  105. else if (size < 1000 * 1000 * 1000)
  106. {
  107. float data = (float)size / 1024 / 1024;
  108. return data.ToString("F2") + " Mb";
  109. }
  110. else
  111. {
  112. float data = (float)size / 1024 / 1024 / 1024;
  113. return data.ToString("F2") + " Gb";
  114. }
  115. }
  116. #endregion
  117. #region TimeSpan Format
  118. /// <summary>
  119. /// 从一个时间差返回带单位的描述
  120. /// </summary>
  121. /// <param name="ts">实际的时间差</param>
  122. /// <returns>最终的字符串值</returns>
  123. public static string GetTimeSpanDescription(TimeSpan ts)
  124. {
  125. if (ts.TotalSeconds <= 60)
  126. {
  127. return (int)ts.TotalSeconds + " 秒";
  128. }
  129. else if (ts.TotalMinutes <= 60)
  130. {
  131. return ts.TotalMinutes.ToString("F1") + " 分钟";
  132. }
  133. else if (ts.TotalHours <= 24)
  134. {
  135. return ts.TotalHours.ToString("F1") + " 小时";
  136. }
  137. else
  138. {
  139. return ts.TotalDays.ToString("F1") + " 天";
  140. }
  141. }
  142. #endregion
  143. #region Array Expaned
  144. /// <summary>
  145. /// 一个通用的数组新增个数方法,会自动判断越界情况,越界的情况下,会自动的截断或是填充 ->
  146. /// A common array of new methods, will automatically determine the cross-border situation, in the case of cross-border, will be automatically truncated or filled
  147. /// </summary>
  148. /// <typeparam name="T">数据类型</typeparam>
  149. /// <param name="array">原数据</param>
  150. /// <param name="data">等待新增的数据</param>
  151. /// <param name="max">原数据的最大值</param>
  152. public static void AddArrayData<T>(ref T[] array, T[] data, int max)
  153. {
  154. if (data == null) return; // 数据为空
  155. if (data.Length == 0) return; // 数据长度为空
  156. if (array.Length == max)
  157. {
  158. Array.Copy(array, data.Length, array, 0, array.Length - data.Length);
  159. Array.Copy(data, 0, array, array.Length - data.Length, data.Length);
  160. }
  161. else
  162. {
  163. if ((array.Length + data.Length) > max)
  164. {
  165. T[] tmp = new T[max];
  166. for (int i = 0; i < (max - data.Length); i++)
  167. {
  168. tmp[i] = array[i + (array.Length - max + data.Length)];
  169. }
  170. for (int i = 0; i < data.Length; i++)
  171. {
  172. tmp[tmp.Length - data.Length + i] = data[i];
  173. }
  174. // 更新数据
  175. array = tmp;
  176. }
  177. else
  178. {
  179. T[] tmp = new T[array.Length + data.Length];
  180. for (int i = 0; i < array.Length; i++)
  181. {
  182. tmp[i] = array[i];
  183. }
  184. for (int i = 0; i < data.Length; i++)
  185. {
  186. tmp[tmp.Length - data.Length + i] = data[i];
  187. }
  188. array = tmp;
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// 将一个数组进行扩充到指定长度,或是缩短到指定长度 ->
  194. /// Extend an array to a specified length, or shorten to a specified length or fill
  195. /// </summary>
  196. /// <typeparam name="T">数组的类型</typeparam>
  197. /// <param name="data">原先数据的数据</param>
  198. /// <param name="length">新数组的长度</param>
  199. /// <returns>新数组长度信息</returns>
  200. public static T[] ArrayExpandToLength<T>(T[] data, int length)
  201. {
  202. if (data == null) return new T[length];
  203. if (data.Length == length) return data;
  204. T[] buffer = new T[length];
  205. Array.Copy(data, buffer, Math.Min(data.Length, buffer.Length));
  206. return buffer;
  207. }
  208. /// <summary>
  209. /// 将一个数组进行扩充到偶数长度 ->
  210. /// Extend an array to even lengths
  211. /// </summary>
  212. /// <typeparam name="T">数组的类型</typeparam>
  213. /// <param name="data">原先数据的数据</param>
  214. /// <returns>新数组长度信息</returns>
  215. public static T[] ArrayExpandToLengthEven<T>(T[] data)
  216. {
  217. if (data == null) return new T[0];
  218. if (data.Length % 2 == 1)
  219. {
  220. return ArrayExpandToLength(data, data.Length + 1);
  221. }
  222. else
  223. {
  224. return data;
  225. }
  226. }
  227. /// <summary>
  228. /// 将指定的数据按照指定长度进行分割,例如int[10],指定长度4,就分割成int[4],int[4],int[2],然后拼接list
  229. /// </summary>
  230. /// <typeparam name="T">数组的类型</typeparam>
  231. /// <param name="array">等待分割的数组</param>
  232. /// <param name="length">指定的长度信息</param>
  233. /// <returns>分割后结果内容</returns>
  234. public static List<T[]> ArraySplitByLength<T>(T[] array, int length)
  235. {
  236. if (array == null) return new List<T[]>();
  237. List<T[]> result = new List<T[]>();
  238. int index = 0;
  239. while (index < array.Length)
  240. {
  241. if (index + length < array.Length)
  242. {
  243. T[] tmp = new T[length];
  244. Array.Copy(array, index, tmp, 0, length);
  245. index += length;
  246. result.Add(tmp);
  247. }
  248. else
  249. {
  250. T[] tmp = new T[array.Length - index];
  251. Array.Copy(array, index, tmp, 0, tmp.Length);
  252. index += length;
  253. result.Add(tmp);
  254. }
  255. }
  256. return result;
  257. }
  258. /// <summary>
  259. /// 将整数进行有效的拆分成数组
  260. /// </summary>
  261. /// <param name="integer">整数信息</param>
  262. /// <param name="everyLength">单个的数组长度</param>
  263. /// <returns>拆分后的数组长度</returns>
  264. public static int[] SplitIntegerToArray(int integer, int everyLength)
  265. {
  266. int[] result = new int[(integer / everyLength) + ((integer % everyLength) == 0 ? 0 : 1)];
  267. for (int i = 0; i < result.Length; i++)
  268. {
  269. if (i == result.Length - 1)
  270. {
  271. result[i] = (integer % everyLength) == 0 ? everyLength : (integer % everyLength);
  272. }
  273. else
  274. {
  275. result[i] = everyLength;
  276. }
  277. }
  278. return result;
  279. }
  280. #endregion
  281. #region Byte Array compare
  282. /// <summary>
  283. /// 判断两个字节的指定部分是否相同 ->
  284. /// Determines whether the specified portion of a two-byte is the same
  285. /// </summary>
  286. /// <param name="b1">第一个字节</param>
  287. /// <param name="start1">第一个字节的起始位置</param>
  288. /// <param name="b2">第二个字节</param>
  289. /// <param name="start2">第二个字节的起始位置</param>
  290. /// <param name="length">校验的长度</param>
  291. /// <returns>返回是否相等</returns>
  292. /// <exception cref="IndexOutOfRangeException"></exception>
  293. public static bool IsTwoBytesEquel(byte[] b1, int start1, byte[] b2, int start2, int length)
  294. {
  295. if (b1 == null || b2 == null) return false;
  296. for (int i = 0; i < length; i++)
  297. {
  298. if (b1[i + start1] != b2[i + start2])
  299. {
  300. return false;
  301. }
  302. }
  303. return true;
  304. }
  305. /// <summary>
  306. /// 判断两个字节的指定部分是否相同 ->
  307. /// Determines whether the specified portion of a two-byte is the same
  308. /// </summary>
  309. /// <param name="b1">第一个字节</param>
  310. /// <param name="b2">第二个字节</param>
  311. /// <returns>返回是否相等</returns>
  312. public static bool IsTwoBytesEquel(byte[] b1, byte[] b2)
  313. {
  314. if (b1 == null || b2 == null) return false;
  315. if (b1.Length != b2.Length) return false;
  316. return IsTwoBytesEquel(b1, 0, b2, 0, b1.Length);
  317. }
  318. /// <summary>
  319. /// 判断两个数据的令牌是否相等 ->
  320. /// Determines whether the tokens of two data are equal
  321. /// </summary>
  322. /// <param name="head">字节数据</param>
  323. /// <param name="token">GUID数据</param>
  324. /// <returns>返回是否相等</returns>
  325. public static bool IsByteTokenEquel(byte[] head, Guid token)
  326. {
  327. return IsTwoBytesEquel(head, 12, token.ToByteArray(), 0, 16);
  328. }
  329. /// <summary>
  330. /// 判断两个数据的令牌是否相等 ->
  331. /// Determines whether the tokens of two data are equal
  332. /// </summary>
  333. /// <param name="token1">第一个令牌</param>
  334. /// <param name="token2">第二个令牌</param>
  335. /// <returns>返回是否相等</returns>
  336. public static bool IsTwoTokenEquel(Guid token1, Guid token2)
  337. {
  338. return IsTwoBytesEquel(token1.ToByteArray(), 0, token2.ToByteArray(), 0, 16);
  339. }
  340. #endregion
  341. #region Enum About
  342. /// <summary>
  343. /// 获取一个枚举类型的所有枚举值,可直接应用于组合框数据 ->
  344. /// Gets all the enumeration values of an enumeration type that can be applied directly to the combo box data
  345. /// </summary>
  346. /// <typeparam name="TEnum">枚举的类型值</typeparam>
  347. /// <returns>枚举值数组</returns>
  348. public static TEnum[] GetEnumValues<TEnum>() where TEnum : struct
  349. {
  350. return (TEnum[])Enum.GetValues(typeof(TEnum));
  351. }
  352. /// <summary>
  353. /// 从字符串的枚举值数据转换成真实的枚举值数据 ->
  354. /// Convert enumeration value data from strings to real enumeration value data
  355. /// </summary>
  356. /// <typeparam name="TEnum">枚举的类型值</typeparam>
  357. /// <param name="value">枚举的字符串的数据值</param>
  358. /// <returns>真实的枚举值</returns>
  359. public static TEnum GetEnumFromString<TEnum>(string value) where TEnum : struct
  360. {
  361. return (TEnum)Enum.Parse(typeof(TEnum), value);
  362. }
  363. #endregion
  364. #region JSON Data Get
  365. /// <summary>
  366. /// 一个泛型方法,提供json对象的数据读取 ->
  367. /// A generic method that provides data read for a JSON object
  368. /// </summary>
  369. /// <typeparam name="T">读取的泛型</typeparam>
  370. /// <param name="json">json对象</param>
  371. /// <param name="value_name">值名称</param>
  372. /// <param name="default_value">默认值</param>
  373. /// <returns>值对象</returns>
  374. public static T GetValueFromJsonObject<T>(JObject json, string value_name, T default_value)
  375. {
  376. if (json.Property(value_name) != null)
  377. {
  378. return json.Property(value_name).Value.Value<T>();
  379. }
  380. else
  381. {
  382. return default_value;
  383. }
  384. }
  385. /// <summary>
  386. /// 一个泛型方法,提供json对象的数据写入 ->
  387. /// A generic method that provides data writing to a JSON object
  388. /// </summary>
  389. /// <typeparam name="T">写入的泛型</typeparam>
  390. /// <param name="json">json对象</param>
  391. /// <param name="property">值名称</param>
  392. /// <param name="value">值数据</param>
  393. public static void JsonSetValue<T>(JObject json, string property, T value)
  394. {
  395. if (json.Property(property) != null)
  396. {
  397. json.Property(property).Value = new JValue(value);
  398. }
  399. else
  400. {
  401. json.Add(property, new JValue(value));
  402. }
  403. }
  404. #endregion
  405. #region Exception Message Format
  406. #if !NETSTANDARD2_0
  407. /// <summary>
  408. /// 显示一个完整的错误信息 ->
  409. /// Displays a complete error message
  410. /// </summary>
  411. /// <param name="ex">异常对象</param>
  412. /// <remarks>调用本方法可以显示一个异常的详细信息</remarks>
  413. /// <exception cref="NullReferenceException"></exception>
  414. public static void ShowExceptionMessage(Exception ex)
  415. {
  416. //MessageBox.Show(GetExceptionMessage(ex));
  417. }
  418. /// <summary>
  419. /// 显示一个完整的错误信息,和额外的字符串描述信息 ->
  420. /// Displays a complete error message, and additional string description information
  421. /// </summary>
  422. /// <param name="extraMsg">额外的描述信息</param>
  423. /// <remarks>调用本方法可以显示一个异常的详细信息</remarks>
  424. /// <param name="ex">异常对象</param>
  425. /// <exception cref="NullReferenceException"></exception>
  426. public static void ShowExceptionMessage(string extraMsg, Exception ex)
  427. {
  428. //MessageBox.Show(GetExceptionMessage(extraMsg, ex));
  429. }
  430. #endif
  431. /// <summary>
  432. /// 获取一个异常的完整错误信息 ->
  433. /// Gets the complete error message for an exception
  434. /// </summary>
  435. /// <param name="ex">异常对象</param>
  436. /// <returns>完整的字符串数据</returns>
  437. /// <remarks>获取异常的完整信息</remarks>
  438. /// <exception cref="NullReferenceException">ex不能为空</exception>
  439. public static string GetExceptionMessage(Exception ex)
  440. {
  441. return StringResources.Language.ExceptionMessage + ex.Message + Environment.NewLine +
  442. StringResources.Language.ExceptionStackTrace + ex.StackTrace + Environment.NewLine +
  443. StringResources.Language.ExceptopnTargetSite + ex.TargetSite;
  444. }
  445. /// <summary>
  446. /// 获取一个异常的完整错误信息,和额外的字符串描述信息 ->
  447. /// Gets the complete error message for an exception, and additional string description information
  448. /// </summary>
  449. /// <param name="extraMsg">额外的信息</param>
  450. /// <param name="ex">异常对象</param>
  451. /// <returns>完整的字符串数据</returns>
  452. /// <exception cref="NullReferenceException"></exception>
  453. public static string GetExceptionMessage(string extraMsg, Exception ex)
  454. {
  455. if (string.IsNullOrEmpty(extraMsg))
  456. {
  457. return GetExceptionMessage(ex);
  458. }
  459. else
  460. {
  461. return extraMsg + Environment.NewLine + GetExceptionMessage(ex);
  462. }
  463. }
  464. #endregion
  465. #region Hex string and Byte[] transform
  466. /// <summary>
  467. /// 字节数据转化成16进制表示的字符串 ->
  468. /// Byte data into a string of 16 binary representations
  469. /// </summary>
  470. /// <param name="InBytes">字节数组</param>
  471. /// <returns>返回的字符串</returns>
  472. /// <exception cref="NullReferenceException"></exception>
  473. public static string ByteToHexString(byte[] InBytes)
  474. {
  475. return ByteToHexString(InBytes, (char)0);
  476. }
  477. /// <summary>
  478. /// 字节数据转化成16进制表示的字符串 ->
  479. /// Byte data into a string of 16 binary representations
  480. /// </summary>
  481. /// <param name="InBytes">字节数组</param>
  482. /// <param name="segment">分割符</param>
  483. /// <returns>返回的字符串</returns>
  484. /// <exception cref="NullReferenceException"></exception>
  485. public static string ByteToHexString(byte[] InBytes, char segment)
  486. {
  487. StringBuilder sb = new StringBuilder();
  488. foreach (byte InByte in InBytes)
  489. {
  490. if (segment == 0) sb.Append(string.Format("{0:X2}", InByte));
  491. else sb.Append(string.Format("{0:X2}{1}", InByte, segment));
  492. }
  493. if (segment != 0 && sb.Length > 1 && sb[sb.Length - 1] == segment)
  494. {
  495. sb.Remove(sb.Length - 1, 1);
  496. }
  497. return sb.ToString();
  498. }
  499. /// <summary>
  500. /// 字符串数据转化成16进制表示的字符串 ->
  501. /// String data into a string of 16 binary representations
  502. /// </summary>
  503. /// <param name="InString">输入的字符串数据</param>
  504. /// <returns>返回的字符串</returns>
  505. /// <exception cref="NullReferenceException"></exception>
  506. public static string ByteToHexString(string InString)
  507. {
  508. return ByteToHexString(Encoding.Unicode.GetBytes(InString));
  509. }
  510. private static List<char> hexCharList = new List<char>()
  511. {
  512. '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
  513. };
  514. /// <summary>
  515. /// 将16进制的字符串转化成Byte数据,将检测每2个字符转化,也就是说,中间可以是任意字符 ->
  516. /// Converts a 16-character string into byte data, which will detect every 2 characters converted, that is, the middle can be any character
  517. /// </summary>
  518. /// <param name="hex">十六进制的字符串,中间可以是任意的分隔符</param>
  519. /// <returns>转换后的字节数组</returns>
  520. /// <remarks>参数举例:AA 01 34 A8</remarks>
  521. public static byte[] HexStringToBytes(string hex)
  522. {
  523. hex = hex.ToUpper();
  524. MemoryStream ms = new MemoryStream();
  525. for (int i = 0; i < hex.Length; i++)
  526. {
  527. if ((i + 1) < hex.Length)
  528. {
  529. if (hexCharList.Contains(hex[i]) && hexCharList.Contains(hex[i + 1]))
  530. {
  531. // 这是一个合格的字节数据
  532. ms.WriteByte((byte)(hexCharList.IndexOf(hex[i]) * 16 + hexCharList.IndexOf(hex[i + 1])));
  533. i++;
  534. }
  535. }
  536. }
  537. byte[] result = ms.ToArray();
  538. ms.Dispose();
  539. return result;
  540. }
  541. #endregion
  542. #region Byte Reverse By Word
  543. /// <summary>
  544. /// 将byte数组按照双字节进行反转,如果为单数的情况,则自动补齐 ->
  545. /// Reverses the byte array by double byte, or if the singular is the case, automatically
  546. /// </summary>
  547. /// <param name="inBytes">输入的字节信息</param>
  548. /// <returns>反转后的数据</returns>
  549. public static byte[] BytesReverseByWord(byte[] inBytes)
  550. {
  551. if (inBytes == null) return null;
  552. byte[] buffer = ArrayExpandToLengthEven(inBytes);
  553. for (int i = 0; i < buffer.Length / 2; i++)
  554. {
  555. byte tmp = buffer[i * 2 + 0];
  556. buffer[i * 2 + 0] = buffer[i * 2 + 1];
  557. buffer[i * 2 + 1] = tmp;
  558. }
  559. return buffer;
  560. }
  561. #endregion
  562. #region Byte[] and AsciiByte[] transform
  563. /// <summary>
  564. /// 将原始的byte数组转换成ascii格式的byte数组 ->
  565. /// Converts the original byte array to an ASCII-formatted byte array
  566. /// </summary>
  567. /// <param name="inBytes">等待转换的byte数组</param>
  568. /// <returns>转换后的数组</returns>
  569. public static byte[] BytesToAsciiBytes(byte[] inBytes)
  570. {
  571. return Encoding.ASCII.GetBytes(ByteToHexString(inBytes));
  572. }
  573. /// <summary>
  574. /// 将ascii格式的byte数组转换成原始的byte数组 ->
  575. /// Converts an ASCII-formatted byte array to the original byte array
  576. /// </summary>
  577. /// <param name="inBytes">等待转换的byte数组</param>
  578. /// <returns>转换后的数组</returns>
  579. public static byte[] AsciiBytesToBytes(byte[] inBytes)
  580. {
  581. return HexStringToBytes(Encoding.ASCII.GetString(inBytes));
  582. }
  583. /// <summary>
  584. /// 从字节构建一个ASCII格式的数据内容
  585. /// </summary>
  586. /// <param name="value">数据</param>
  587. /// <returns>ASCII格式的字节数组</returns>
  588. public static byte[] BuildAsciiBytesFrom(byte value)
  589. {
  590. return Encoding.ASCII.GetBytes(value.ToString("X2"));
  591. }
  592. /// <summary>
  593. /// 从short构建一个ASCII格式的数据内容
  594. /// </summary>
  595. /// <param name="value">数据</param>
  596. /// <returns>ASCII格式的字节数组</returns>
  597. public static byte[] BuildAsciiBytesFrom(short value)
  598. {
  599. return Encoding.ASCII.GetBytes(value.ToString("X4"));
  600. }
  601. /// <summary>
  602. /// 从ushort构建一个ASCII格式的数据内容
  603. /// </summary>
  604. /// <param name="value">数据</param>
  605. /// <returns>ASCII格式的字节数组</returns>
  606. public static byte[] BuildAsciiBytesFrom(ushort value)
  607. {
  608. return Encoding.ASCII.GetBytes(value.ToString("X4"));
  609. }
  610. /// <summary>
  611. /// 从字节数组构建一个ASCII格式的数据内容
  612. /// </summary>
  613. /// <param name="value">字节信息</param>
  614. /// <returns>ASCII格式的地址</returns>
  615. public static byte[] BuildAsciiBytesFrom(byte[] value)
  616. {
  617. byte[] buffer = new byte[value.Length * 2];
  618. for (int i = 0; i < value.Length; i++)
  619. {
  620. SoftBasic.BuildAsciiBytesFrom(value[i]).CopyTo(buffer, 2 * i);
  621. }
  622. return buffer;
  623. }
  624. #endregion
  625. #region Bool[] and byte[] transform
  626. /// <summary>
  627. /// 将bool数组转换到byte数组 ->
  628. /// Converting a bool array to a byte array
  629. /// </summary>
  630. /// <param name="array">bool数组</param>
  631. /// <returns>转换后的字节数组</returns>
  632. public static byte[] BoolArrayToByte(bool[] array)
  633. {
  634. if (array == null) return null;
  635. int length = array.Length % 8 == 0 ? array.Length / 8 : array.Length / 8 + 1;
  636. byte[] buffer = new byte[length];
  637. for (int i = 0; i < array.Length; i++)
  638. {
  639. int index = i / 8;
  640. int offect = i % 8;
  641. byte temp = 0;
  642. switch (offect)
  643. {
  644. case 0: temp = 0x01; break;
  645. case 1: temp = 0x02; break;
  646. case 2: temp = 0x04; break;
  647. case 3: temp = 0x08; break;
  648. case 4: temp = 0x10; break;
  649. case 5: temp = 0x20; break;
  650. case 6: temp = 0x40; break;
  651. case 7: temp = 0x80; break;
  652. default: break;
  653. }
  654. if (array[i]) buffer[index] += temp;
  655. }
  656. return buffer;
  657. }
  658. /// <summary>
  659. /// 从Byte数组中提取位数组,length代表位数 ->
  660. /// Extracts a bit array from a byte array, length represents the number of digits
  661. /// </summary>
  662. /// <param name="InBytes">原先的字节数组</param>
  663. /// <param name="length">想要转换的长度,如果超出自动会缩小到数组最大长度</param>
  664. /// <returns>转换后的bool数组</returns>
  665. public static bool[] ByteToBoolArray(byte[] InBytes, int length)
  666. {
  667. if (InBytes == null) return null;
  668. if (length > InBytes.Length * 8) length = InBytes.Length * 8;
  669. bool[] buffer = new bool[length];
  670. for (int i = 0; i < length; i++)
  671. {
  672. int index = i / 8;
  673. int offect = i % 8;
  674. byte temp = 0;
  675. switch (offect)
  676. {
  677. case 0: temp = 0x01; break;
  678. case 1: temp = 0x02; break;
  679. case 2: temp = 0x04; break;
  680. case 3: temp = 0x08; break;
  681. case 4: temp = 0x10; break;
  682. case 5: temp = 0x20; break;
  683. case 6: temp = 0x40; break;
  684. case 7: temp = 0x80; break;
  685. default: break;
  686. }
  687. if ((InBytes[index] & temp) == temp)
  688. {
  689. buffer[i] = true;
  690. }
  691. }
  692. return buffer;
  693. }
  694. /// <summary>
  695. /// 从Byte数组中提取所有的位数组 ->
  696. /// Extracts a bit array from a byte array, length represents the number of digits
  697. /// </summary>
  698. /// <param name="InBytes">原先的字节数组</param>
  699. /// <returns>转换后的bool数组</returns>
  700. public static bool[] ByteToBoolArray(byte[] InBytes)
  701. {
  702. if (InBytes == null) return null;
  703. return ByteToBoolArray(InBytes, InBytes.Length * 8);
  704. }
  705. #endregion
  706. #region Byte[] Splice
  707. /// <summary>
  708. /// 拼接2个字节数组成一个数组 ->
  709. /// Splicing 2 bytes to to an array
  710. /// </summary>
  711. /// <param name="bytes1">数组一</param>
  712. /// <param name="bytes2">数组二</param>
  713. /// <returns>拼接后的数组</returns>
  714. public static byte[] SpliceTwoByteArray(byte[] bytes1, byte[] bytes2)
  715. {
  716. if (bytes1 == null && bytes2 == null) return null;
  717. if (bytes1 == null) return bytes2;
  718. if (bytes2 == null) return bytes1;
  719. byte[] buffer = new byte[bytes1.Length + bytes2.Length];
  720. bytes1.CopyTo(buffer, 0);
  721. bytes2.CopyTo(buffer, bytes1.Length);
  722. return buffer;
  723. }
  724. /// <summary>
  725. /// 选择一个byte数组的前面的几个byte数据信息
  726. /// </summary>
  727. /// <param name="value">原始的数据信息</param>
  728. /// <param name="length">数据的长度</param>
  729. /// <returns>选择的前面的几个数据信息</returns>
  730. public static byte[] BytesArraySelectBegin(byte[] value, int length)
  731. {
  732. byte[] buffer = new byte[Math.Min(value.Length, length)];
  733. Array.Copy(value, 0, buffer, 0, buffer.Length);
  734. return buffer;
  735. }
  736. /// <summary>
  737. /// 将一个byte数组的前面指定位数移除,返回新的一个数组 ->
  738. /// Removes the preceding specified number of bits in a byte array, returning a new array
  739. /// </summary>
  740. /// <param name="value">字节数组</param>
  741. /// <param name="length">等待移除的长度</param>
  742. /// <returns>新的数据</returns>
  743. public static byte[] BytesArrayRemoveBegin(byte[] value, int length)
  744. {
  745. return BytesArrayRemoveDouble(value, length, 0);
  746. }
  747. /// <summary>
  748. /// 将一个byte数组的后面指定位数移除,返回新的一个数组 ->
  749. /// Removes the specified number of digits after a byte array, returning a new array
  750. /// </summary>
  751. /// <param name="value">字节数组</param>
  752. /// <param name="length">等待移除的长度</param>
  753. /// <returns>新的数据</returns>
  754. public static byte[] BytesArrayRemoveLast(byte[] value, int length)
  755. {
  756. return BytesArrayRemoveDouble(value, 0, length);
  757. }
  758. /// <summary>
  759. /// 将一个byte数组的前后移除指定位数,返回新的一个数组 ->
  760. /// Removes a byte array before and after the specified number of bits, returning a new array
  761. /// </summary>
  762. /// <param name="value">字节数组</param>
  763. /// <param name="leftLength">前面的位数</param>
  764. /// <param name="rightLength">后面的位数</param>
  765. /// <returns>新的数据</returns>
  766. public static byte[] BytesArrayRemoveDouble(byte[] value, int leftLength, int rightLength)
  767. {
  768. if (value == null) return null;
  769. if (value.Length <= (leftLength + rightLength)) return new byte[0];
  770. byte[] buffer = new byte[value.Length - leftLength - rightLength];
  771. Array.Copy(value, leftLength, buffer, 0, buffer.Length);
  772. return buffer;
  773. }
  774. #endregion
  775. #region Basic Framework
  776. /// <summary>
  777. /// 设置或获取系统框架的版本号 ->
  778. /// Set or get the version number of the system framework
  779. /// </summary>
  780. /// <remarks>
  781. /// 当你要显示本组件框架的版本号的时候,就可以用这个属性来显示
  782. /// </remarks>
  783. //public static SystemVersion FrameworkVersion { get; set; } = new SystemVersion("7.0.0");
  784. #endregion
  785. #region Deep Clone
  786. /// <summary>
  787. /// 使用序列化反序列化深度克隆一个对象,该对象需要支持序列化特性 ->
  788. /// Cloning an object with serialization deserialization depth that requires support for serialization attributes
  789. /// </summary>
  790. /// <param name="oringinal">源对象,支持序列化</param>
  791. /// <returns>新的一个实例化的对象</returns>
  792. /// <exception cref="NullReferenceException"></exception>
  793. /// <exception cref="NonSerializedAttribute"></exception>
  794. /// <remarks>
  795. /// <note type="warning">
  796. /// <paramref name="oringinal"/> 参数必须实现序列化的特性
  797. /// </note>
  798. /// </remarks>
  799. public static object DeepClone(object oringinal)
  800. {
  801. using (MemoryStream stream = new MemoryStream())
  802. {
  803. BinaryFormatter formatter = new BinaryFormatter()
  804. {
  805. Context = new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Clone)
  806. };
  807. formatter.Serialize(stream, oringinal);
  808. stream.Position = 0;
  809. return formatter.Deserialize(stream);
  810. }
  811. }
  812. #endregion
  813. #region Unique String Get
  814. /// <summary>
  815. /// 获取一串唯一的随机字符串,长度为20,由Guid码和4位数的随机数组成,保证字符串的唯一性 ->
  816. /// Gets a string of unique random strings with a length of 20, consisting of a GUID code and a 4-digit random number to guarantee the uniqueness of the string
  817. /// </summary>
  818. /// <returns>随机字符串数据</returns>
  819. public static string GetUniqueStringByGuidAndRandom()
  820. {
  821. Random random = new Random();
  822. return Guid.NewGuid().ToString("N") + random.Next(1000, 10000);
  823. }
  824. #endregion
  825. }
  826. }