BigEndianByteTransformBase.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MECF.Framework.Common.Net
  6. {
  7. /// <summary>
  8. /// 数据转换类的基础,提供了一些基础的方法实现.
  9. /// </summary>
  10. public class BigEndianByteTransformBase : IByteTransform
  11. {
  12. #region 常量
  13. private const int BYTE_BIT_LENGTH = 8;
  14. #endregion
  15. #region 属性
  16. public DataFormat DataFormat { get; set; } = DataFormat.Big_Endian;
  17. #endregion
  18. #region Get Value From Bytes
  19. /// <summary>
  20. /// 从缓存中提取出bool结果
  21. /// </summary>
  22. /// <param name="buffer">缓存数据</param>
  23. /// <param name="index">位的索引</param>
  24. /// <returns>bool对象</returns>
  25. public bool TransBool( byte[] buffer, int index )
  26. {
  27. return BitConverter.ToBoolean(buffer, index);
  28. }
  29. /// <summary>
  30. /// 从缓存中提取出bool数组结果
  31. /// </summary>
  32. /// <param name="buffer">缓存数据</param>
  33. /// <param name="index">位的索引</param>
  34. /// <param name="length">bool长度</param>
  35. /// <returns>bool数组</returns>
  36. public bool[] TransBool( byte[] buffer, int index, int length )
  37. {
  38. byte[] tmp = new byte[length];
  39. Array.Copy( buffer, index, tmp, 0, length );
  40. bool[] boolByt = new bool[length * BYTE_BIT_LENGTH];
  41. for (int i = 0; i < length; i++)
  42. {
  43. bool[] tmpBoolByt = GetBoolByByte(tmp[i]);
  44. Array.Copy(tmpBoolByt, 0, boolByt, i * BYTE_BIT_LENGTH, tmpBoolByt.Length);
  45. }
  46. return boolByt;
  47. }
  48. /// <summary>
  49. /// 获取bool数组
  50. /// </summary>
  51. /// <param name="byt"></param>
  52. /// <returns></returns>
  53. private bool[] GetBoolByByte(byte byt)
  54. {
  55. bool[] bytes = new bool[BYTE_BIT_LENGTH];
  56. bytes[0] =(bool)((byt & 0x01) == 0x01);
  57. for(int i=1;i<bytes.Length;i++)
  58. {
  59. bytes[i] = (bool)((byt>>i & 0x01) == 0x01);
  60. }
  61. return bytes;
  62. }
  63. /// <summary>
  64. /// 从缓存中提取byte结果
  65. /// </summary>
  66. /// <param name="buffer">缓存数据</param>
  67. /// <param name="index">索引位置</param>
  68. /// <returns>byte对象</returns>
  69. public byte TransByte( byte[] buffer, int index )
  70. {
  71. return buffer[index];
  72. }
  73. /// <summary>
  74. /// 从缓存中提取byte数组结果
  75. /// </summary>
  76. /// <param name="buffer">缓存数据</param>
  77. /// <param name="index">索引位置</param>
  78. /// <param name="length">读取的数组长度</param>
  79. /// <returns>byte数组对象</returns>
  80. public byte[] TransByte( byte[] buffer, int index, int length )
  81. {
  82. byte[] tmp = new byte[length];
  83. Array.Copy( buffer, index, tmp, 0, length );
  84. return tmp;
  85. }
  86. /// <summary>
  87. /// 从缓存中提取short结果
  88. /// </summary>
  89. /// <param name="buffer">缓存数据</param>
  90. /// <param name="index">索引位置</param>
  91. /// <returns>short对象</returns>
  92. public short TransInt16( byte[] buffer, int index )
  93. {
  94. byte[] bytes = ReverseArray(buffer, index, 2);
  95. return BitConverter.ToInt16(bytes,0);
  96. }
  97. /// <summary>
  98. /// 逆转数组
  99. /// </summary>
  100. /// <param name="buffer"></param>
  101. /// <param name="index"></param>
  102. /// <param name="length"></param>
  103. /// <returns></returns>
  104. private byte[] ReverseArray(byte[] buffer,int index,int length)
  105. {
  106. byte[] bytes= new byte[length];
  107. Array.Copy(buffer,index,bytes, 0, length);
  108. Array.Reverse(bytes);
  109. return bytes;
  110. }
  111. /// <summary>
  112. /// 从缓存中提取short数组结果
  113. /// </summary>
  114. /// <param name="buffer">缓存数据</param>
  115. /// <param name="index">索引位置</param>
  116. /// <param name="length">读取的数组长度</param>
  117. /// <returns>short数组对象</returns>
  118. public short[] TransInt16( byte[] buffer, int index, int length )
  119. {
  120. short[] tmp = new short[length];
  121. for (int i = 0; i < length; i++)
  122. {
  123. tmp[i] = TransInt16( buffer, index + 2 * i );
  124. }
  125. return tmp;
  126. }
  127. /// <summary>
  128. /// 从缓存中提取ushort结果
  129. /// </summary>
  130. /// <param name="buffer">缓存数据</param>
  131. /// <param name="index">索引位置</param>
  132. /// <returns>ushort对象</returns>
  133. public ushort TransUInt16( byte[] buffer, int index )
  134. {
  135. byte[] bytes = ReverseArray(buffer, index, 2);
  136. return BitConverter.ToUInt16(bytes, 0);
  137. }
  138. /// <summary>
  139. /// 从缓存中提取ushort数组结果
  140. /// </summary>
  141. /// <param name="buffer">缓存数据</param>
  142. /// <param name="index">索引位置</param>
  143. /// <param name="length">读取的数组长度</param>
  144. /// <returns>ushort数组对象</returns>
  145. public ushort[] TransUInt16( byte[] buffer, int index, int length )
  146. {
  147. ushort[] tmp = new ushort[length];
  148. for (int i = 0; i < length; i++)
  149. {
  150. tmp[i] = TransUInt16( buffer, index + 2 * i );
  151. }
  152. return tmp;
  153. }
  154. /// <summary>
  155. /// 从缓存中提取int结果
  156. /// </summary>
  157. /// <param name="buffer">缓存数据</param>
  158. /// <param name="index">索引位置</param>
  159. /// <returns>int对象</returns>
  160. public int TransInt32( byte[] buffer, int index )
  161. {
  162. byte[] bytes = ReverseArray(buffer, index, 4);
  163. return BitConverter.ToInt32(bytes, 0);
  164. }
  165. /// <summary>
  166. /// 从缓存中提取int数组结果
  167. /// </summary>
  168. /// <param name="buffer">缓存数据</param>
  169. /// <param name="index">索引位置</param>
  170. /// <param name="length">读取的数组长度</param>
  171. /// <returns>int数组对象</returns>
  172. public int[] TransInt32( byte[] buffer, int index, int length )
  173. {
  174. int[] tmp = new int[length];
  175. for (int i = 0; i < length; i++)
  176. {
  177. tmp[i] = TransInt32( buffer, index + 4 * i );
  178. }
  179. return tmp;
  180. }
  181. /// <summary>
  182. /// 从缓存中提取uint结果
  183. /// </summary>
  184. /// <param name="buffer">缓存数据</param>
  185. /// <param name="index">索引位置</param>
  186. /// <returns>uint对象</returns>
  187. public uint TransUInt32( byte[] buffer, int index )
  188. {
  189. byte[] bytes = ReverseArray(buffer, index, 4);
  190. return BitConverter.ToUInt32(bytes, 0);
  191. }
  192. /// <summary>
  193. /// 从缓存中提取uint数组结果
  194. /// </summary>
  195. /// <param name="buffer">缓存数据</param>
  196. /// <param name="index">索引位置</param>
  197. /// <param name="length">读取的数组长度</param>
  198. /// <returns>uint数组对象</returns>
  199. public uint[] TransUInt32( byte[] buffer, int index, int length )
  200. {
  201. uint[] tmp = new uint[length];
  202. for (int i = 0; i < length; i++)
  203. {
  204. tmp[i] = TransUInt32( buffer, index + 4 * i );
  205. }
  206. return tmp;
  207. }
  208. /// <summary>
  209. /// 从缓存中提取long结果
  210. /// </summary>
  211. /// <param name="buffer">缓存数据</param>
  212. /// <param name="index">索引位置</param>
  213. /// <returns>long对象</returns>
  214. public long TransInt64( byte[] buffer, int index )
  215. {
  216. byte[] bytes = ReverseArray(buffer, index, 8);
  217. return BitConverter.ToInt64(bytes, 0);
  218. }
  219. /// <summary>
  220. /// 从缓存中提取long数组结果
  221. /// </summary>
  222. /// <param name="buffer">缓存数据</param>
  223. /// <param name="index">索引位置</param>
  224. /// <param name="length">读取的数组长度</param>
  225. /// <returns>long数组对象</returns>
  226. public long[] TransInt64( byte[] buffer, int index, int length )
  227. {
  228. long[] tmp = new long[length];
  229. for (int i = 0; i < length; i++)
  230. {
  231. tmp[i] = TransInt64( buffer, index + 8 * i );
  232. }
  233. return tmp;
  234. }
  235. /// <summary>
  236. /// 从缓存中提取ulong结果
  237. /// </summary>
  238. /// <param name="buffer">缓存数据</param>
  239. /// <param name="index">索引位置</param>
  240. /// <returns>ulong对象</returns>
  241. public ulong TransUInt64( byte[] buffer, int index )
  242. {
  243. byte[] bytes = ReverseArray(buffer, index, 8);
  244. return BitConverter.ToUInt64(bytes, 0);
  245. }
  246. /// <summary>
  247. /// 从缓存中提取ulong数组结果
  248. /// </summary>
  249. /// <param name="buffer">缓存数据</param>
  250. /// <param name="index">索引位置</param>
  251. /// <param name="length">读取的数组长度</param>
  252. /// <returns>ulong数组对象</returns>
  253. public ulong[] TransUInt64( byte[] buffer, int index, int length )
  254. {
  255. ulong[] tmp = new ulong[length];
  256. for (int i = 0; i < length; i++)
  257. {
  258. tmp[i] = TransUInt64( buffer, index + 8 * i );
  259. }
  260. return tmp;
  261. }
  262. /// <summary>
  263. /// 从缓存中提取float结果
  264. /// </summary>
  265. /// <param name="buffer">缓存对象</param>
  266. /// <param name="index">索引位置</param>
  267. /// <returns>float对象</returns>
  268. public float TransSingle( byte[] buffer, int index )
  269. {
  270. byte[] bytes = ReverseArray(buffer, index, 4);
  271. return BitConverter.ToSingle(bytes, 0);
  272. }
  273. /// <summary>
  274. /// 从缓存中提取float数组结果
  275. /// </summary>
  276. /// <param name="buffer">缓存数据</param>
  277. /// <param name="index">索引位置</param>
  278. /// <param name="length">读取的数组长度</param>
  279. /// <returns>float数组对象</returns>
  280. public float[] TransSingle( byte[] buffer, int index, int length )
  281. {
  282. float[] tmp = new float[length];
  283. for (int i = 0; i < length; i++)
  284. {
  285. tmp[i] = TransSingle( buffer, index + 4 * i );
  286. }
  287. return tmp;
  288. }
  289. /// <summary>
  290. /// 从缓存中提取double结果
  291. /// </summary>
  292. /// <param name="buffer">缓存对象</param>
  293. /// <param name="index">索引位置</param>
  294. /// <returns>double对象</returns>
  295. public double TransDouble( byte[] buffer, int index )
  296. {
  297. byte[] bytes = ReverseArray(buffer, index, 8);
  298. return BitConverter.ToDouble(bytes, 0);
  299. }
  300. /// <summary>
  301. /// 从缓存中提取double数组结果
  302. /// </summary>
  303. /// <param name="buffer">缓存对象</param>
  304. /// <param name="index">索引位置</param>
  305. /// <param name="length">读取的数组长度</param>
  306. /// <returns>double数组对象</returns>
  307. public double[] TransDouble( byte[] buffer, int index, int length )
  308. {
  309. double[] tmp = new double[length];
  310. for (int i = 0; i < length; i++)
  311. {
  312. tmp[i] = TransDouble( buffer, index + 8 * i );
  313. }
  314. return tmp;
  315. }
  316. /// <summary>
  317. /// 从缓存中提取string结果,使用指定的编码
  318. /// </summary>
  319. /// <param name="buffer">缓存对象</param>
  320. /// <param name="index">索引位置</param>
  321. /// <param name="length">byte数组长度</param>
  322. /// <param name="encoding">字符串的编码</param>
  323. /// <returns>string对象</returns>
  324. public virtual string TransString( byte[] buffer, int index, int length, Encoding encoding )
  325. {
  326. byte[] tmp = TransByte( buffer, index, length );
  327. return encoding.GetString( tmp );
  328. }
  329. #endregion
  330. #region Get Bytes From Value
  331. /// <summary>
  332. /// bool变量转化缓存数据
  333. /// </summary>
  334. /// <param name="value">等待转化的数据</param>
  335. /// <returns>buffer数据</returns>
  336. public byte[] GetBytes( bool value )
  337. {
  338. return BitConverter.GetBytes( value );
  339. }
  340. /// <summary>
  341. /// bool数组变量转化缓存数据
  342. /// </summary>
  343. /// <param name="values">等待转化的数组</param>
  344. /// <returns>buffer数据</returns>
  345. public byte[] GetBytes( bool[] values )
  346. {
  347. int length = values.Length % BYTE_BIT_LENGTH == 0 ? values.Length / BYTE_BIT_LENGTH : values.Length / BYTE_BIT_LENGTH + 1;
  348. byte[] bytes= new byte[length];
  349. for(int i = 0; i < length; i++)
  350. {
  351. bool[] boolByt = null;
  352. if(i==length-1)
  353. {
  354. boolByt=new bool[values.Length-i*BYTE_BIT_LENGTH];
  355. }
  356. else
  357. {
  358. boolByt = new bool[BYTE_BIT_LENGTH];
  359. }
  360. Array.Copy(values, i * BYTE_BIT_LENGTH, boolByt, 0, boolByt.Length);
  361. bytes[i/BYTE_BIT_LENGTH]=GetByteByBoolArray( boolByt );
  362. }
  363. return bytes;
  364. }
  365. /// <summary>
  366. /// 根据bit位数组获取Byte数据
  367. /// </summary>
  368. /// <param name="boolAry"></param>
  369. /// <returns></returns>
  370. private byte GetByteByBoolArray(bool[] boolAry)
  371. {
  372. byte byt = 0;
  373. for(int i=0;i<boolAry.Length;i++)
  374. {
  375. byt += (byte)(boolAry[i]?1:0 << i);
  376. }
  377. return byt;
  378. }
  379. /// <summary>
  380. /// byte变量转化缓存数据
  381. /// </summary>
  382. /// <param name="value">等待转化的数据</param>
  383. /// <returns>buffer数据</returns>
  384. public byte[] GetBytes( byte value )
  385. {
  386. return new byte[] { value };
  387. }
  388. /// <summary>
  389. /// short变量转化缓存数据
  390. /// </summary>
  391. /// <param name="value">等待转化的数据</param>
  392. /// <returns>buffer数据</returns>
  393. public byte[] GetBytes(short value)
  394. {
  395. byte[] bytes= BitConverter.GetBytes(value);
  396. Array.Reverse(bytes);
  397. return bytes;
  398. }
  399. /// <summary>
  400. /// short数组变量转化缓存数据
  401. /// </summary>
  402. /// <param name="values">等待转化的数组</param>
  403. /// <returns>buffer数据</returns>
  404. public byte[] GetBytes( short[] values )
  405. {
  406. byte[] buffer = new byte[values.Length * 2];
  407. for (int i = 0; i < values.Length; i++)
  408. {
  409. GetBytes( values[i] ).CopyTo( buffer, 2 * i );
  410. }
  411. return buffer;
  412. }
  413. /// <summary>
  414. /// ushort变量转化缓存数据
  415. /// </summary>
  416. /// <param name="value">等待转化的数据</param>
  417. /// <returns>buffer数据</returns>
  418. public byte[] GetBytes( ushort value )
  419. {
  420. byte[] bytes = BitConverter.GetBytes(value);
  421. Array.Reverse(bytes);
  422. return bytes;
  423. }
  424. /// <summary>
  425. /// ushort数组变量转化缓存数据
  426. /// </summary>
  427. /// <param name="values">等待转化的数组</param>
  428. /// <returns>buffer数据</returns>
  429. public byte[] GetBytes( ushort[] values )
  430. {
  431. byte[] buffer = new byte[values.Length * 2];
  432. for (int i = 0; i < values.Length; i++)
  433. {
  434. GetBytes( values[i] ).CopyTo( buffer, 2 * i );
  435. }
  436. return buffer;
  437. }
  438. /// <summary>
  439. /// int变量转化缓存数据
  440. /// </summary>
  441. /// <param name="value">等待转化的数据</param>
  442. /// <returns>buffer数据</returns>
  443. public byte[] GetBytes( int value )
  444. {
  445. byte[] bytes = BitConverter.GetBytes(value);
  446. Array.Reverse(bytes);
  447. return bytes;
  448. }
  449. /// <summary>
  450. /// int数组变量转化缓存数据
  451. /// </summary>
  452. /// <param name="values">等待转化的数组</param>
  453. /// <returns>buffer数据</returns>
  454. public byte[] GetBytes( int[] values )
  455. {
  456. byte[] buffer = new byte[values.Length * 4];
  457. for (int i = 0; i < values.Length; i++)
  458. {
  459. GetBytes(values[i]).CopyTo( buffer, 4 * i );
  460. }
  461. return buffer;
  462. }
  463. /// <summary>
  464. /// uint变量转化缓存数据
  465. /// </summary>
  466. /// <param name="value">等待转化的数据</param>
  467. /// <returns>buffer数据</returns>
  468. public byte[] GetBytes( uint value )
  469. {
  470. byte[] bytes = BitConverter.GetBytes(value);
  471. Array.Reverse(bytes);
  472. return bytes;
  473. }
  474. /// <summary>
  475. /// uint数组变量转化缓存数据
  476. /// </summary>
  477. /// <param name="values">等待转化的数组</param>
  478. /// <returns>buffer数据</returns>
  479. public byte[] GetBytes( uint[] values )
  480. {
  481. byte[] buffer = new byte[values.Length * 4];
  482. for (int i = 0; i < values.Length; i++)
  483. {
  484. GetBytes(values[i]).CopyTo( buffer, 4 * i );
  485. }
  486. return buffer;
  487. }
  488. /// <summary>
  489. /// long变量转化缓存数据
  490. /// </summary>
  491. /// <param name="value">等待转化的数据</param>
  492. /// <returns>buffer数据</returns>
  493. public byte[] GetBytes( long value )
  494. {
  495. byte[] bytes = BitConverter.GetBytes(value);
  496. Array.Reverse(bytes);
  497. return bytes;
  498. }
  499. /// <summary>
  500. /// long数组变量转化缓存数据
  501. /// </summary>
  502. /// <param name="values">等待转化的数组</param>
  503. /// <returns>buffer数据</returns>
  504. public byte[] GetBytes( long[] values )
  505. {
  506. byte[] buffer = new byte[values.Length * 8];
  507. for (int i = 0; i < values.Length; i++)
  508. {
  509. GetBytes(values[i]).CopyTo( buffer, 8 * i );
  510. }
  511. return buffer;
  512. }
  513. /// <summary>
  514. /// ulong变量转化缓存数据
  515. /// </summary>
  516. /// <param name="value">等待转化的数据</param>
  517. /// <returns>buffer数据</returns>
  518. public byte[] GetBytes( ulong value )
  519. {
  520. byte[] bytes = BitConverter.GetBytes(value);
  521. Array.Reverse(bytes);
  522. return bytes;
  523. }
  524. /// <summary>
  525. /// ulong数组变量转化缓存数据
  526. /// </summary>
  527. /// <param name="values">等待转化的数组</param>
  528. /// <returns>buffer数据</returns>
  529. public byte[] GetBytes( ulong[] values )
  530. {
  531. if (values == null) return null;
  532. byte[] buffer = new byte[values.Length * 8];
  533. for (int i = 0; i < values.Length; i++)
  534. {
  535. GetBytes(values[i]).CopyTo( buffer, 8 * i );
  536. }
  537. return buffer;
  538. }
  539. /// <summary>
  540. /// float变量转化缓存数据
  541. /// </summary>
  542. /// <param name="value">等待转化的数据</param>
  543. /// <returns>buffer数据</returns>
  544. public byte[] GetBytes( float value )
  545. {
  546. byte[] bytes = BitConverter.GetBytes(value);
  547. Array.Reverse(bytes);
  548. return bytes;
  549. }
  550. /// <summary>
  551. /// float数组变量转化缓存数据
  552. /// </summary>
  553. /// <param name="values">等待转化的数组</param>
  554. /// <returns>buffer数据</returns>
  555. public byte[] GetBytes( float[] values )
  556. {
  557. if (values == null) return null;
  558. byte[] buffer = new byte[values.Length * 4];
  559. for (int i = 0; i < values.Length; i++)
  560. {
  561. GetBytes(values[i]).CopyTo( buffer, 4 * i );
  562. }
  563. return buffer;
  564. }
  565. /// <summary>
  566. /// double变量转化缓存数据
  567. /// </summary>
  568. /// <param name="value">等待转化的数据</param>
  569. /// <returns>buffer数据</returns>
  570. public byte[] GetBytes( double value )
  571. {
  572. byte[] bytes = BitConverter.GetBytes(value);
  573. Array.Reverse(bytes);
  574. return bytes;
  575. }
  576. /// <summary>
  577. /// double数组变量转化缓存数据
  578. /// </summary>
  579. /// <param name="values">等待转化的数组</param>
  580. /// <returns>buffer数据</returns>
  581. public byte[] GetBytes( double[] values )
  582. {
  583. if (values == null) return null;
  584. byte[] buffer = new byte[values.Length * 8];
  585. for (int i = 0; i < values.Length; i++)
  586. {
  587. GetBytes( values[i] ).CopyTo( buffer, 8 * i );
  588. }
  589. return buffer;
  590. }
  591. /// <summary>
  592. /// 使用指定的编码字符串转化缓存数据
  593. /// </summary>
  594. /// <param name="value">等待转化的数据</param>
  595. /// <param name="encoding">字符串的编码方式</param>
  596. /// <returns>buffer数据</returns>
  597. public byte[] GetBytes( string value, Encoding encoding )
  598. {
  599. return encoding.GetBytes( value );
  600. }
  601. #endregion
  602. }
  603. }