using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HslCommunication.Core { /// /// 按照字节错位的数据转换类 /// public class ReverseWordTransform : IByteTransform { #region Private Method /// /// 按照字节错位的方法 /// /// /// /// /// private byte[] ReverseBytesByWord( byte[] buffer, int index, int length ) { byte[] tmp = new byte[length]; for (int i = 0; i < length; i++) { tmp[i] = buffer[index + i]; } for (int i = 0; i < length / 2; i++) { byte b = tmp[i * 2 + 0]; tmp[i * 2 + 0] = tmp[i * 2 + 1]; tmp[i * 2 + 1] = b; } return tmp; } private byte[] ReverseBytesByWord( byte[] buffer ) { return ReverseBytesByWord( buffer, 0, buffer.Length ); } #endregion #region Get Value From Bytes /// /// 从缓存中提取出bool结果 /// /// 缓存数据 /// bool对象 public bool TransBool( byte[] buffer ) { return buffer[0] != 0x00; } /// /// 从缓存中提取byte结果 /// /// 缓存数据 /// 索引位置 /// byte对象 public byte TransByte( byte[] buffer, int index ) { return buffer[index]; } /// /// 从缓存中提取short结果 /// /// 缓存数据 /// 索引位置 /// short对象 public short TransInt16( byte[] buffer, int index ) { return BitConverter.ToInt16( ReverseBytesByWord( buffer, index, 2 ), 0 ); } /// /// 从缓存中提取ushort结果 /// /// 缓存数据 /// 索引位置 /// ushort对象 public ushort TransUInt16( byte[] buffer, int index ) { return BitConverter.ToUInt16( ReverseBytesByWord( buffer, index, 2 ), 0 ); } /// /// 从缓存中提取int结果 /// /// 缓存数据 /// 索引位置 /// int对象 public int TransInt32( byte[] buffer, int index ) { return BitConverter.ToInt32( ReverseBytesByWord( buffer, index, 4 ), 0 ); } /// /// 从缓存中提取uint结果 /// /// 缓存数据 /// 索引位置 /// uint对象 public uint TransUInt32( byte[] buffer, int index ) { return BitConverter.ToUInt32( ReverseBytesByWord( buffer, index, 4 ), 0 ); } /// /// 从缓存中提取long结果 /// /// 缓存数据 /// 索引位置 /// long对象 public long TransInt64( byte[] buffer, int index ) { return BitConverter.ToInt64( ReverseBytesByWord( buffer, index, 8 ), 0 ); } /// /// 从缓存中提取ulong结果 /// /// 缓存数据 /// 索引位置 /// ulong对象 public ulong TransUInt64( byte[] buffer, int index ) { return BitConverter.ToUInt64( ReverseBytesByWord( buffer, index, 8 ), 0 ); } /// /// 从缓存中提取float结果 /// /// 缓存对象 /// 索引位置 /// float对象 public float TransSingle( byte[] buffer, int index ) { return BitConverter.ToSingle( ReverseBytesByWord( buffer, index, 4 ), 0 ); } /// /// 从缓存中提取double结果 /// /// 缓存对象 /// 索引位置 /// double对象 public double TransDouble( byte[] buffer, int index ) { return BitConverter.ToDouble( ReverseBytesByWord( buffer, index, 8 ), 0 ); } /// /// 从缓存中提取string结果,编码ASCII /// /// 缓存对象 /// string对象 public string TransString( byte[] buffer ) { return Encoding.ASCII.GetString( buffer ); } #endregion #region Get Bytes From Value /// /// bool变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( bool value ) { return value ? new byte[1] { 0x01 } : new byte[1] { 0x00 }; } /// /// bool数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public byte[] TransByte( bool[] values ) { return BasicFramework.SoftBasic.BoolArrayToByte( values ); } /// /// byte变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( byte value ) { return new byte[1] { value }; } /// /// short变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( short value ) { return TransByte( new short[] { value } ); } /// /// short数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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] ); tmp.CopyTo( buffer, 2 * i ); } return ReverseBytesByWord( buffer ); } /// /// ushort变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( ushort value ) { return TransByte( new ushort[] { value } ); } /// /// ushort数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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] ); tmp.CopyTo( buffer, 2 * i ); } return ReverseBytesByWord( buffer ); } /// /// int变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( int value ) { return TransByte( new int[] { value } ); } /// /// int数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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++) { BitConverter.GetBytes( values[i] ).CopyTo( buffer, 4 * i ); } return ReverseBytesByWord( buffer ); } /// /// uint变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( uint value ) { return TransByte( new uint[] { value } ); } /// /// uint数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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++) { BitConverter.GetBytes( values[i] ).CopyTo( buffer, 4 * i ); } return ReverseBytesByWord( buffer ); } /// /// long变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( long value ) { return TransByte( new long[] { value } ); } /// /// long数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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 ReverseBytesByWord( buffer ); } /// /// ulong变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( ulong value ) { return TransByte( new ulong[] { value } ); } /// /// ulong数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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 ReverseBytesByWord( buffer ); } /// /// float变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( float value ) { return TransByte( new float[] { value } ); } /// /// float数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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 ReverseBytesByWord( buffer ); } /// /// double变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( double value ) { return TransByte( new double[] { value } ); } /// /// double数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 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 ReverseBytesByWord( buffer ); } /// /// ASCII编码字符串转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public byte[] TransByte( string value ) { return Encoding.ASCII.GetBytes( value ); } #endregion } }