ReverseBytesTransform.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. /**********************************************************************************************
  6. *
  7. * 说明:一般的转换类
  8. * 日期:2018年3月14日 17:05:30
  9. *
  10. **********************************************************************************************/
  11. namespace HslCommunication.Core
  12. {
  13. /// <summary>
  14. /// 字节倒序的转换类
  15. /// </summary>
  16. public class ReverseBytesTransform : IByteTransform
  17. {
  18. #region Get Value From Bytes
  19. /// <summary>
  20. /// 从缓存中提取出bool结果
  21. /// </summary>
  22. /// <param name="buffer">缓存数据</param>
  23. /// <returns>bool对象</returns>
  24. public bool TransBool( byte[] buffer )
  25. {
  26. return buffer[0] != 0x00;
  27. }
  28. /// <summary>
  29. /// 从缓存中提取byte结果
  30. /// </summary>
  31. /// <param name="buffer">缓存数据</param>
  32. /// <param name="index">索引位置</param>
  33. /// <returns>byte对象</returns>
  34. public byte TransByte( byte[] buffer, int index )
  35. {
  36. return buffer[index];
  37. }
  38. /// <summary>
  39. /// 从缓存中提取short结果
  40. /// </summary>
  41. /// <param name="buffer">缓存数据</param>
  42. /// <param name="index">索引位置</param>
  43. /// <returns>short对象</returns>
  44. public short TransInt16( byte[] buffer, int index )
  45. {
  46. byte[] tmp = new byte[2];
  47. tmp[0] = buffer[1 + index];
  48. tmp[1] = buffer[0 + index];
  49. return BitConverter.ToInt16( tmp, 0 );
  50. }
  51. /// <summary>
  52. /// 从缓存中提取ushort结果
  53. /// </summary>
  54. /// <param name="buffer">缓存数据</param>
  55. /// <param name="index">索引位置</param>
  56. /// <returns>ushort对象</returns>
  57. public ushort TransUInt16( byte[] buffer, int index )
  58. {
  59. byte[] tmp = new byte[2];
  60. tmp[0] = buffer[1 + index];
  61. tmp[1] = buffer[0 + index];
  62. return BitConverter.ToUInt16( tmp, 0 );
  63. }
  64. /// <summary>
  65. /// 从缓存中提取int结果
  66. /// </summary>
  67. /// <param name="buffer">缓存数据</param>
  68. /// <param name="index">索引位置</param>
  69. /// <returns>int对象</returns>
  70. public int TransInt32( byte[] buffer, int index )
  71. {
  72. byte[] tmp = new byte[4];
  73. tmp[0] = buffer[3 + index];
  74. tmp[1] = buffer[2 + index];
  75. tmp[2] = buffer[1 + index];
  76. tmp[3] = buffer[0 + index];
  77. return BitConverter.ToInt32( tmp, 0 );
  78. }
  79. /// <summary>
  80. /// 从缓存中提取uint结果
  81. /// </summary>
  82. /// <param name="buffer">缓存数据</param>
  83. /// <param name="index">索引位置</param>
  84. /// <returns>uint对象</returns>
  85. public uint TransUInt32( byte[] buffer, int index )
  86. {
  87. byte[] tmp = new byte[4];
  88. tmp[0] = buffer[3 + index];
  89. tmp[1] = buffer[2 + index];
  90. tmp[2] = buffer[1 + index];
  91. tmp[3] = buffer[0 + index];
  92. return BitConverter.ToUInt32( tmp, 0 );
  93. }
  94. /// <summary>
  95. /// 从缓存中提取long结果
  96. /// </summary>
  97. /// <param name="buffer">缓存数据</param>
  98. /// <param name="index">索引位置</param>
  99. /// <returns>long对象</returns>
  100. public long TransInt64( byte[] buffer, int index )
  101. {
  102. byte[] tmp = new byte[8];
  103. tmp[0] = buffer[7 + index];
  104. tmp[1] = buffer[6 + index];
  105. tmp[2] = buffer[5 + index];
  106. tmp[3] = buffer[4 + index];
  107. tmp[4] = buffer[3 + index];
  108. tmp[5] = buffer[2 + index];
  109. tmp[6] = buffer[1 + index];
  110. tmp[7] = buffer[0 + index];
  111. return BitConverter.ToInt64( tmp, 0 );
  112. }
  113. /// <summary>
  114. /// 从缓存中提取ulong结果
  115. /// </summary>
  116. /// <param name="buffer">缓存数据</param>
  117. /// <param name="index">索引位置</param>
  118. /// <returns>ulong对象</returns>
  119. public ulong TransUInt64( byte[] buffer, int index )
  120. {
  121. byte[] tmp = new byte[8];
  122. tmp[0] = buffer[7 + index];
  123. tmp[1] = buffer[6 + index];
  124. tmp[2] = buffer[5 + index];
  125. tmp[3] = buffer[4 + index];
  126. tmp[4] = buffer[3 + index];
  127. tmp[5] = buffer[2 + index];
  128. tmp[6] = buffer[1 + index];
  129. tmp[7] = buffer[0 + index];
  130. return BitConverter.ToUInt64( tmp, 0 );
  131. }
  132. /// <summary>
  133. /// 从缓存中提取float结果
  134. /// </summary>
  135. /// <param name="buffer">缓存对象</param>
  136. /// <param name="index">索引位置</param>
  137. /// <returns>float对象</returns>
  138. public float TransSingle( byte[] buffer, int index )
  139. {
  140. byte[] tmp = new byte[4];
  141. tmp[0] = buffer[3 + index];
  142. tmp[1] = buffer[2 + index];
  143. tmp[2] = buffer[1 + index];
  144. tmp[3] = buffer[0 + index];
  145. return BitConverter.ToSingle( tmp, 0 );
  146. }
  147. /// <summary>
  148. /// 从缓存中提取double结果
  149. /// </summary>
  150. /// <param name="buffer">缓存对象</param>
  151. /// <param name="index">索引位置</param>
  152. /// <returns>double对象</returns>
  153. public double TransDouble( byte[] buffer, int index )
  154. {
  155. byte[] tmp = new byte[8];
  156. tmp[0] = buffer[7 + index];
  157. tmp[1] = buffer[6 + index];
  158. tmp[2] = buffer[5 + index];
  159. tmp[3] = buffer[4 + index];
  160. tmp[4] = buffer[3 + index];
  161. tmp[5] = buffer[2 + index];
  162. tmp[6] = buffer[1 + index];
  163. tmp[7] = buffer[0 + index];
  164. return BitConverter.ToDouble( tmp, 0 );
  165. }
  166. /// <summary>
  167. /// 从缓存中提取string结果,编码ASCII
  168. /// </summary>
  169. /// <param name="buffer">缓存对象</param>
  170. /// <returns>string对象</returns>
  171. public string TransString( byte[] buffer )
  172. {
  173. return Encoding.ASCII.GetString( buffer );
  174. }
  175. #endregion
  176. #region Get Bytes From Value
  177. /// <summary>
  178. /// bool变量转化缓存数据
  179. /// </summary>
  180. /// <param name="value">等待转化的数据</param>
  181. /// <returns>buffer数据</returns>
  182. public byte[] TransByte( bool value )
  183. {
  184. return TransByte( new bool[] { value } );
  185. }
  186. /// <summary>
  187. /// bool数组变量转化缓存数据
  188. /// </summary>
  189. /// <param name="values">等待转化的数组</param>
  190. /// <returns>buffer数据</returns>
  191. public byte[] TransByte( bool[] values )
  192. {
  193. if (values == null) return null;
  194. byte[] buffer = new byte[values.Length];
  195. for (int i = 0; i < values.Length; i++)
  196. {
  197. if (values[i]) buffer[i] = 0x01;
  198. }
  199. return buffer;
  200. }
  201. /// <summary>
  202. /// byte变量转化缓存数据
  203. /// </summary>
  204. /// <param name="value">等待转化的数据</param>
  205. /// <returns>buffer数据</returns>
  206. public byte[] TransByte( byte value )
  207. {
  208. return new byte[] { value };
  209. }
  210. /// <summary>
  211. /// short变量转化缓存数据
  212. /// </summary>
  213. /// <param name="value">等待转化的数据</param>
  214. /// <returns>buffer数据</returns>
  215. public byte[] TransByte( short value )
  216. {
  217. return TransByte( new short[] { value } );
  218. }
  219. /// <summary>
  220. /// short数组变量转化缓存数据
  221. /// </summary>
  222. /// <param name="values">等待转化的数组</param>
  223. /// <returns>buffer数据</returns>
  224. public byte[] TransByte( short[] values )
  225. {
  226. if (values == null) return null;
  227. byte[] buffer = new byte[values.Length * 2];
  228. for (int i = 0; i < values.Length; i++)
  229. {
  230. byte[] tmp = BitConverter.GetBytes( values[i] );
  231. Array.Reverse( tmp );
  232. tmp.CopyTo( buffer, 2 * i );
  233. }
  234. return buffer;
  235. }
  236. /// <summary>
  237. /// ushort变量转化缓存数据
  238. /// </summary>
  239. /// <param name="value">等待转化的数据</param>
  240. /// <returns>buffer数据</returns>
  241. public byte[] TransByte( ushort value )
  242. {
  243. return TransByte( new ushort[] { value } );
  244. }
  245. /// <summary>
  246. /// ushort数组变量转化缓存数据
  247. /// </summary>
  248. /// <param name="values">等待转化的数组</param>
  249. /// <returns>buffer数据</returns>
  250. public byte[] TransByte( ushort[] values )
  251. {
  252. if (values == null) return null;
  253. byte[] buffer = new byte[values.Length * 2];
  254. for (int i = 0; i < values.Length; i++)
  255. {
  256. byte[] tmp = BitConverter.GetBytes( values[i] );
  257. Array.Reverse( tmp );
  258. tmp.CopyTo( buffer, 2 * i );
  259. }
  260. return buffer;
  261. }
  262. /// <summary>
  263. /// int变量转化缓存数据
  264. /// </summary>
  265. /// <param name="value">等待转化的数据</param>
  266. /// <returns>buffer数据</returns>
  267. public byte[] TransByte( int value )
  268. {
  269. return TransByte( new int[] { value } );
  270. }
  271. /// <summary>
  272. /// int数组变量转化缓存数据
  273. /// </summary>
  274. /// <param name="values">等待转化的数组</param>
  275. /// <returns>buffer数据</returns>
  276. public byte[] TransByte( int[] values )
  277. {
  278. if (values == null) return null;
  279. byte[] buffer = new byte[values.Length * 4];
  280. for (int i = 0; i < values.Length; i++)
  281. {
  282. byte[] tmp = BitConverter.GetBytes( values[i] );
  283. Array.Reverse( tmp );
  284. tmp.CopyTo( buffer, 4 * i );
  285. }
  286. return buffer;
  287. }
  288. /// <summary>
  289. /// uint变量转化缓存数据
  290. /// </summary>
  291. /// <param name="value">等待转化的数据</param>
  292. /// <returns>buffer数据</returns>
  293. public byte[] TransByte( uint value )
  294. {
  295. return TransByte( new uint[] { value } );
  296. }
  297. /// <summary>
  298. /// uint数组变量转化缓存数据
  299. /// </summary>
  300. /// <param name="values">等待转化的数组</param>
  301. /// <returns>buffer数据</returns>
  302. public byte[] TransByte( uint[] values )
  303. {
  304. if (values == null) return null;
  305. byte[] buffer = new byte[values.Length * 4];
  306. for (int i = 0; i < values.Length; i++)
  307. {
  308. byte[] tmp = BitConverter.GetBytes( values[i] );
  309. Array.Reverse( tmp );
  310. tmp.CopyTo( buffer, 4 * i );
  311. }
  312. return buffer;
  313. }
  314. /// <summary>
  315. /// long变量转化缓存数据
  316. /// </summary>
  317. /// <param name="value">等待转化的数据</param>
  318. /// <returns>buffer数据</returns>
  319. public byte[] TransByte( long value )
  320. {
  321. return TransByte( new long[] { value } );
  322. }
  323. /// <summary>
  324. /// long数组变量转化缓存数据
  325. /// </summary>
  326. /// <param name="values">等待转化的数组</param>
  327. /// <returns>buffer数据</returns>
  328. public byte[] TransByte( long[] values )
  329. {
  330. if (values == null) return null;
  331. byte[] buffer = new byte[values.Length * 8];
  332. for (int i = 0; i < values.Length; i++)
  333. {
  334. BitConverter.GetBytes( values[i] ).CopyTo( buffer, 8 * i );
  335. }
  336. return buffer;
  337. }
  338. /// <summary>
  339. /// ulong变量转化缓存数据
  340. /// </summary>
  341. /// <param name="value">等待转化的数据</param>
  342. /// <returns>buffer数据</returns>
  343. public byte[] TransByte( ulong value )
  344. {
  345. return TransByte( new ulong[] { value } );
  346. }
  347. /// <summary>
  348. /// ulong数组变量转化缓存数据
  349. /// </summary>
  350. /// <param name="values">等待转化的数组</param>
  351. /// <returns>buffer数据</returns>
  352. public byte[] TransByte( ulong[] values )
  353. {
  354. if (values == null) return null;
  355. byte[] buffer = new byte[values.Length * 8];
  356. for (int i = 0; i < values.Length; i++)
  357. {
  358. BitConverter.GetBytes( values[i] ).CopyTo( buffer, 8 * i );
  359. }
  360. return buffer;
  361. }
  362. /// <summary>
  363. /// float变量转化缓存数据
  364. /// </summary>
  365. /// <param name="value">等待转化的数据</param>
  366. /// <returns>buffer数据</returns>
  367. public byte[] TransByte( float value )
  368. {
  369. return TransByte( new float[] { value } );
  370. }
  371. /// <summary>
  372. /// float数组变量转化缓存数据
  373. /// </summary>
  374. /// <param name="values">等待转化的数组</param>
  375. /// <returns>buffer数据</returns>
  376. public byte[] TransByte( float[] values )
  377. {
  378. if (values == null) return null;
  379. byte[] buffer = new byte[values.Length * 4];
  380. for (int i = 0; i < values.Length; i++)
  381. {
  382. BitConverter.GetBytes( values[i] ).CopyTo( buffer, 4 * i );
  383. }
  384. return buffer;
  385. }
  386. /// <summary>
  387. /// double变量转化缓存数据
  388. /// </summary>
  389. /// <param name="value">等待转化的数据</param>
  390. /// <returns>buffer数据</returns>
  391. public byte[] TransByte( double value )
  392. {
  393. return TransByte( new double[] { value } );
  394. }
  395. /// <summary>
  396. /// double数组变量转化缓存数据
  397. /// </summary>
  398. /// <param name="values">等待转化的数组</param>
  399. /// <returns>buffer数据</returns>
  400. public byte[] TransByte( double[] values )
  401. {
  402. if (values == null) return null;
  403. byte[] buffer = new byte[values.Length * 8];
  404. for (int i = 0; i < values.Length; i++)
  405. {
  406. BitConverter.GetBytes( values[i] ).CopyTo( buffer, 8 * i );
  407. }
  408. return buffer;
  409. }
  410. /// <summary>
  411. /// ASCII编码字符串转化缓存数据
  412. /// </summary>
  413. /// <param name="value">等待转化的数据</param>
  414. /// <returns>buffer数据</returns>
  415. public byte[] TransByte( string value )
  416. {
  417. if (value == null) return null;
  418. return Encoding.ASCII.GetBytes( value );
  419. }
  420. #endregion
  421. }
  422. }