SoftBasic.cs 36 KB

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