| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- /**********************************************************************************************
- *
- * 说明:一般的转换类
- * 日期:2018年3月14日 17:05:30
- *
- **********************************************************************************************/
- namespace HslCommunication.Core
- {
- /// <summary>
- /// 字节倒序的转换类
- /// </summary>
- public class ReverseBytesTransform : IByteTransform
- {
- #region Get Value From Bytes
- /// <summary>
- /// 从缓存中提取出bool结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <returns>bool对象</returns>
- public bool TransBool( byte[] buffer )
- {
- return buffer[0] != 0x00;
- }
- /// <summary>
- /// 从缓存中提取byte结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <param name="index">索引位置</param>
- /// <returns>byte对象</returns>
- public byte TransByte( byte[] buffer, int index )
- {
- return buffer[index];
- }
- /// <summary>
- /// 从缓存中提取short结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <param name="index">索引位置</param>
- /// <returns>short对象</returns>
- public short TransInt16( byte[] buffer, int index )
- {
- byte[] tmp = new byte[2];
- tmp[0] = buffer[1 + index];
- tmp[1] = buffer[0 + index];
- return BitConverter.ToInt16( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取ushort结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <param name="index">索引位置</param>
- /// <returns>ushort对象</returns>
- public ushort TransUInt16( byte[] buffer, int index )
- {
- byte[] tmp = new byte[2];
- tmp[0] = buffer[1 + index];
- tmp[1] = buffer[0 + index];
- return BitConverter.ToUInt16( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取int结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <param name="index">索引位置</param>
- /// <returns>int对象</returns>
- public int TransInt32( byte[] buffer, int index )
- {
- byte[] tmp = new byte[4];
- tmp[0] = buffer[3 + index];
- tmp[1] = buffer[2 + index];
- tmp[2] = buffer[1 + index];
- tmp[3] = buffer[0 + index];
- return BitConverter.ToInt32( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取uint结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <param name="index">索引位置</param>
- /// <returns>uint对象</returns>
- public uint TransUInt32( byte[] buffer, int index )
- {
- byte[] tmp = new byte[4];
- tmp[0] = buffer[3 + index];
- tmp[1] = buffer[2 + index];
- tmp[2] = buffer[1 + index];
- tmp[3] = buffer[0 + index];
- return BitConverter.ToUInt32( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取long结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <param name="index">索引位置</param>
- /// <returns>long对象</returns>
- public long TransInt64( byte[] buffer, int index )
- {
- byte[] tmp = new byte[8];
- tmp[0] = buffer[7 + index];
- tmp[1] = buffer[6 + index];
- tmp[2] = buffer[5 + index];
- tmp[3] = buffer[4 + index];
- tmp[4] = buffer[3 + index];
- tmp[5] = buffer[2 + index];
- tmp[6] = buffer[1 + index];
- tmp[7] = buffer[0 + index];
- return BitConverter.ToInt64( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取ulong结果
- /// </summary>
- /// <param name="buffer">缓存数据</param>
- /// <param name="index">索引位置</param>
- /// <returns>ulong对象</returns>
- public ulong TransUInt64( byte[] buffer, int index )
- {
- byte[] tmp = new byte[8];
- tmp[0] = buffer[7 + index];
- tmp[1] = buffer[6 + index];
- tmp[2] = buffer[5 + index];
- tmp[3] = buffer[4 + index];
- tmp[4] = buffer[3 + index];
- tmp[5] = buffer[2 + index];
- tmp[6] = buffer[1 + index];
- tmp[7] = buffer[0 + index];
- return BitConverter.ToUInt64( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取float结果
- /// </summary>
- /// <param name="buffer">缓存对象</param>
- /// <param name="index">索引位置</param>
- /// <returns>float对象</returns>
- public float TransSingle( byte[] buffer, int index )
- {
- byte[] tmp = new byte[4];
- tmp[0] = buffer[3 + index];
- tmp[1] = buffer[2 + index];
- tmp[2] = buffer[1 + index];
- tmp[3] = buffer[0 + index];
- return BitConverter.ToSingle( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取double结果
- /// </summary>
- /// <param name="buffer">缓存对象</param>
- /// <param name="index">索引位置</param>
- /// <returns>double对象</returns>
- public double TransDouble( byte[] buffer, int index )
- {
- byte[] tmp = new byte[8];
- tmp[0] = buffer[7 + index];
- tmp[1] = buffer[6 + index];
- tmp[2] = buffer[5 + index];
- tmp[3] = buffer[4 + index];
- tmp[4] = buffer[3 + index];
- tmp[5] = buffer[2 + index];
- tmp[6] = buffer[1 + index];
- tmp[7] = buffer[0 + index];
- return BitConverter.ToDouble( tmp, 0 );
- }
- /// <summary>
- /// 从缓存中提取string结果,编码ASCII
- /// </summary>
- /// <param name="buffer">缓存对象</param>
- /// <returns>string对象</returns>
- public string TransString( byte[] buffer )
- {
- return Encoding.ASCII.GetString( buffer );
- }
- #endregion
- #region Get Bytes From Value
- /// <summary>
- /// bool变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( bool value )
- {
- return TransByte( new bool[] { value } );
- }
- /// <summary>
- /// bool数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( bool[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length];
- for (int i = 0; i < values.Length; i++)
- {
- if (values[i]) buffer[i] = 0x01;
- }
- return buffer;
- }
- /// <summary>
- /// byte变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( byte value )
- {
- return new byte[] { value };
- }
- /// <summary>
- /// short变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( short value )
- {
- return TransByte( new short[] { value } );
- }
- /// <summary>
- /// short数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( short[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 2];
- for (int i = 0; i < values.Length; i++)
- {
- byte[] tmp = BitConverter.GetBytes( values[i] );
- Array.Reverse( tmp );
- tmp.CopyTo( buffer, 2 * i );
- }
- return buffer;
- }
- /// <summary>
- /// ushort变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( ushort value )
- {
- return TransByte( new ushort[] { value } );
- }
- /// <summary>
- /// ushort数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( ushort[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 2];
- for (int i = 0; i < values.Length; i++)
- {
- byte[] tmp = BitConverter.GetBytes( values[i] );
- Array.Reverse( tmp );
- tmp.CopyTo( buffer, 2 * i );
- }
- return buffer;
- }
- /// <summary>
- /// int变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( int value )
- {
- return TransByte( new int[] { value } );
- }
- /// <summary>
- /// int数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( int[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 4];
- for (int i = 0; i < values.Length; i++)
- {
- byte[] tmp = BitConverter.GetBytes( values[i] );
- Array.Reverse( tmp );
- tmp.CopyTo( buffer, 4 * i );
- }
- return buffer;
- }
- /// <summary>
- /// uint变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( uint value )
- {
- return TransByte( new uint[] { value } );
- }
- /// <summary>
- /// uint数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( uint[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 4];
- for (int i = 0; i < values.Length; i++)
- {
- byte[] tmp = BitConverter.GetBytes( values[i] );
- Array.Reverse( tmp );
- tmp.CopyTo( buffer, 4 * i );
- }
- return buffer;
- }
- /// <summary>
- /// long变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( long value )
- {
- return TransByte( new long[] { value } );
- }
- /// <summary>
- /// long数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( long[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 8];
- for (int i = 0; i < values.Length; i++)
- {
- BitConverter.GetBytes( values[i] ).CopyTo( buffer, 8 * i );
- }
- return buffer;
- }
- /// <summary>
- /// ulong变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( ulong value )
- {
- return TransByte( new ulong[] { value } );
- }
- /// <summary>
- /// ulong数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( ulong[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 8];
- for (int i = 0; i < values.Length; i++)
- {
- BitConverter.GetBytes( values[i] ).CopyTo( buffer, 8 * i );
- }
- return buffer;
- }
- /// <summary>
- /// float变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( float value )
- {
- return TransByte( new float[] { value } );
- }
- /// <summary>
- /// float数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( float[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 4];
- for (int i = 0; i < values.Length; i++)
- {
- BitConverter.GetBytes( values[i] ).CopyTo( buffer, 4 * i );
- }
- return buffer;
- }
- /// <summary>
- /// double变量转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( double value )
- {
- return TransByte( new double[] { value } );
- }
- /// <summary>
- /// double数组变量转化缓存数据
- /// </summary>
- /// <param name="values">等待转化的数组</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( double[] values )
- {
- if (values == null) return null;
- byte[] buffer = new byte[values.Length * 8];
- for (int i = 0; i < values.Length; i++)
- {
- BitConverter.GetBytes( values[i] ).CopyTo( buffer, 8 * i );
- }
- return buffer;
- }
- /// <summary>
- /// ASCII编码字符串转化缓存数据
- /// </summary>
- /// <param name="value">等待转化的数据</param>
- /// <returns>buffer数据</returns>
- public byte[] TransByte( string value )
- {
- if (value == null) return null;
- return Encoding.ASCII.GetBytes( value );
- }
- #endregion
- }
- }
|