SmallEndianByteTransformBase.cs 20 KB

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