using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/**********************************************************************************************
*
* 说明:一般的转换类
* 日期:2018年3月14日 17:05:30
*
**********************************************************************************************/
namespace HslCommunication.Core
{
///
/// 常规的字节转换类
///
public class RegularByteTransform : IByteTransform
{
#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( buffer, index );
}
///
/// 从缓存中提取ushort结果
///
/// 缓存数据
/// 索引位置
/// ushort对象
public ushort TransUInt16( byte[] buffer, int index )
{
return BitConverter.ToUInt16( buffer, index );
}
///
/// 从缓存中提取int结果
///
/// 缓存数据
/// 索引位置
/// int对象
public int TransInt32( byte[] buffer, int index )
{
return BitConverter.ToInt32( buffer, index );
}
///
/// 从缓存中提取uint结果
///
/// 缓存数据
/// 索引位置
/// uint对象
public uint TransUInt32( byte[] buffer, int index )
{
return BitConverter.ToUInt32( buffer, index );
}
///
/// 从缓存中提取long结果
///
/// 缓存数据
/// 索引位置
/// long对象
public long TransInt64( byte[] buffer, int index )
{
return BitConverter.ToInt64( buffer, index );
}
///
/// 从缓存中提取ulong结果
///
/// 缓存数据
/// 索引位置
/// ulong对象
public ulong TransUInt64( byte[] buffer, int index )
{
return BitConverter.ToUInt64( buffer, index );
}
///
/// 从缓存中提取float结果
///
/// 缓存对象
/// 索引位置
/// float对象
public float TransSingle( byte[] buffer, int index )
{
return BitConverter.ToSingle( buffer, index );
}
///
/// 从缓存中提取double结果
///
/// 缓存对象
/// 索引位置
/// double对象
public double TransDouble( byte[] buffer, int index )
{
return BitConverter.ToDouble( buffer, index );
}
///
/// 从缓存中提取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 TransByte( new bool[] { value } );
}
///
/// bool数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
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;
}
///
/// byte变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] TransByte( byte value )
{
return new byte[] { 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++)
{
BitConverter.GetBytes( values[i] ).CopyTo( buffer, 2 * i );
}
return 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++)
{
BitConverter.GetBytes( values[i] ).CopyTo( buffer, 2 * i );
}
return 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 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 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 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 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 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 buffer;
}
///
/// ASCII编码字符串转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] TransByte( string value )
{
if (value == null) return null;
return Encoding.ASCII.GetBytes( value );
}
#endregion
}
}