using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MECF.Framework.Common.Net
{
///
/// 数据转换类的基础,提供了一些基础的方法实现.
///
public class BigEndianByteTransformBase : IByteTransform
{
#region 常量
private const int BYTE_BIT_LENGTH = 8;
#endregion
#region 属性
public DataFormat DataFormat { get; set; } = DataFormat.Big_Endian;
#endregion
#region Get Value From Bytes
///
/// 从缓存中提取出bool结果
///
/// 缓存数据
/// 位的索引
/// bool对象
public bool TransBool( byte[] buffer, int index )
{
return BitConverter.ToBoolean(buffer, index);
}
///
/// 从缓存中提取出bool数组结果
///
/// 缓存数据
/// 位的索引
/// bool长度
/// bool数组
public bool[] TransBool( byte[] buffer, int index, int length )
{
byte[] tmp = new byte[length];
Array.Copy( buffer, index, tmp, 0, length );
bool[] boolByt = new bool[length * BYTE_BIT_LENGTH];
for (int i = 0; i < length; i++)
{
bool[] tmpBoolByt = GetBoolByByte(tmp[i]);
Array.Copy(tmpBoolByt, 0, boolByt, i * BYTE_BIT_LENGTH, tmpBoolByt.Length);
}
return boolByt;
}
///
/// 获取bool数组
///
///
///
private bool[] GetBoolByByte(byte byt)
{
bool[] bytes = new bool[BYTE_BIT_LENGTH];
bytes[0] =(bool)((byt & 0x01) == 0x01);
for(int i=1;i>i & 0x01) == 0x01);
}
return bytes;
}
///
/// 从缓存中提取byte结果
///
/// 缓存数据
/// 索引位置
/// byte对象
public byte TransByte( byte[] buffer, int index )
{
return buffer[index];
}
///
/// 从缓存中提取byte数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// byte数组对象
public byte[] TransByte( byte[] buffer, int index, int length )
{
byte[] tmp = new byte[length];
Array.Copy( buffer, index, tmp, 0, length );
return tmp;
}
///
/// 从缓存中提取short结果
///
/// 缓存数据
/// 索引位置
/// short对象
public short TransInt16( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 2);
return BitConverter.ToInt16(bytes,0);
}
///
/// 逆转数组
///
///
///
///
///
private byte[] ReverseArray(byte[] buffer,int index,int length)
{
byte[] bytes= new byte[length];
Array.Copy(buffer,index,bytes, 0, length);
Array.Reverse(bytes);
return bytes;
}
///
/// 从缓存中提取short数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// short数组对象
public short[] TransInt16( byte[] buffer, int index, int length )
{
short[] tmp = new short[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransInt16( buffer, index + 2 * i );
}
return tmp;
}
///
/// 从缓存中提取ushort结果
///
/// 缓存数据
/// 索引位置
/// ushort对象
public ushort TransUInt16( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 2);
return BitConverter.ToUInt16(bytes, 0);
}
///
/// 从缓存中提取ushort数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// ushort数组对象
public ushort[] TransUInt16( byte[] buffer, int index, int length )
{
ushort[] tmp = new ushort[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransUInt16( buffer, index + 2 * i );
}
return tmp;
}
///
/// 从缓存中提取int结果
///
/// 缓存数据
/// 索引位置
/// int对象
public int TransInt32( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 4);
return BitConverter.ToInt32(bytes, 0);
}
///
/// 从缓存中提取int数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// int数组对象
public int[] TransInt32( byte[] buffer, int index, int length )
{
int[] tmp = new int[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransInt32( buffer, index + 4 * i );
}
return tmp;
}
///
/// 从缓存中提取uint结果
///
/// 缓存数据
/// 索引位置
/// uint对象
public uint TransUInt32( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 4);
return BitConverter.ToUInt32(bytes, 0);
}
///
/// 从缓存中提取uint数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// uint数组对象
public uint[] TransUInt32( byte[] buffer, int index, int length )
{
uint[] tmp = new uint[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransUInt32( buffer, index + 4 * i );
}
return tmp;
}
///
/// 从缓存中提取long结果
///
/// 缓存数据
/// 索引位置
/// long对象
public long TransInt64( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 8);
return BitConverter.ToInt64(bytes, 0);
}
///
/// 从缓存中提取long数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// long数组对象
public long[] TransInt64( byte[] buffer, int index, int length )
{
long[] tmp = new long[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransInt64( buffer, index + 8 * i );
}
return tmp;
}
///
/// 从缓存中提取ulong结果
///
/// 缓存数据
/// 索引位置
/// ulong对象
public ulong TransUInt64( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 8);
return BitConverter.ToUInt64(bytes, 0);
}
///
/// 从缓存中提取ulong数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// ulong数组对象
public ulong[] TransUInt64( byte[] buffer, int index, int length )
{
ulong[] tmp = new ulong[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransUInt64( buffer, index + 8 * i );
}
return tmp;
}
///
/// 从缓存中提取float结果
///
/// 缓存对象
/// 索引位置
/// float对象
public float TransSingle( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 4);
return BitConverter.ToSingle(bytes, 0);
}
///
/// 从缓存中提取float数组结果
///
/// 缓存数据
/// 索引位置
/// 读取的数组长度
/// float数组对象
public float[] TransSingle( byte[] buffer, int index, int length )
{
float[] tmp = new float[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransSingle( buffer, index + 4 * i );
}
return tmp;
}
///
/// 从缓存中提取double结果
///
/// 缓存对象
/// 索引位置
/// double对象
public double TransDouble( byte[] buffer, int index )
{
byte[] bytes = ReverseArray(buffer, index, 8);
return BitConverter.ToDouble(bytes, 0);
}
///
/// 从缓存中提取double数组结果
///
/// 缓存对象
/// 索引位置
/// 读取的数组长度
/// double数组对象
public double[] TransDouble( byte[] buffer, int index, int length )
{
double[] tmp = new double[length];
for (int i = 0; i < length; i++)
{
tmp[i] = TransDouble( buffer, index + 8 * i );
}
return tmp;
}
///
/// 从缓存中提取string结果,使用指定的编码
///
/// 缓存对象
/// 索引位置
/// byte数组长度
/// 字符串的编码
/// string对象
public virtual string TransString( byte[] buffer, int index, int length, Encoding encoding )
{
byte[] tmp = TransByte( buffer, index, length );
return encoding.GetString( tmp );
}
#endregion
#region Get Bytes From Value
///
/// bool变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( bool value )
{
return BitConverter.GetBytes( value );
}
///
/// bool数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( bool[] values )
{
int length = values.Length % BYTE_BIT_LENGTH == 0 ? values.Length / BYTE_BIT_LENGTH : values.Length / BYTE_BIT_LENGTH + 1;
byte[] bytes= new byte[length];
for(int i = 0; i < length; i++)
{
bool[] boolByt = null;
if(i==length-1)
{
boolByt=new bool[values.Length-i*BYTE_BIT_LENGTH];
}
else
{
boolByt = new bool[BYTE_BIT_LENGTH];
}
Array.Copy(values, i * BYTE_BIT_LENGTH, boolByt, 0, boolByt.Length);
bytes[i/BYTE_BIT_LENGTH]=GetByteByBoolArray( boolByt );
}
return bytes;
}
///
/// 根据bit位数组获取Byte数据
///
///
///
private byte GetByteByBoolArray(bool[] boolAry)
{
byte byt = 0;
for(int i=0;i
/// byte变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( byte value )
{
return new byte[] { value };
}
///
/// short变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes(short value)
{
byte[] bytes= BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// short数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( short[] values )
{
byte[] buffer = new byte[values.Length * 2];
for (int i = 0; i < values.Length; i++)
{
GetBytes( values[i] ).CopyTo( buffer, 2 * i );
}
return buffer;
}
///
/// ushort变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( ushort value )
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// ushort数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( ushort[] values )
{
byte[] buffer = new byte[values.Length * 2];
for (int i = 0; i < values.Length; i++)
{
GetBytes( values[i] ).CopyTo( buffer, 2 * i );
}
return buffer;
}
///
/// int变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( int value )
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// int数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( int[] values )
{
byte[] buffer = new byte[values.Length * 4];
for (int i = 0; i < values.Length; i++)
{
GetBytes(values[i]).CopyTo( buffer, 4 * i );
}
return buffer;
}
///
/// uint变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( uint value )
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// uint数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( uint[] values )
{
byte[] buffer = new byte[values.Length * 4];
for (int i = 0; i < values.Length; i++)
{
GetBytes(values[i]).CopyTo( buffer, 4 * i );
}
return buffer;
}
///
/// long变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( long value )
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// long数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( long[] values )
{
byte[] buffer = new byte[values.Length * 8];
for (int i = 0; i < values.Length; i++)
{
GetBytes(values[i]).CopyTo( buffer, 8 * i );
}
return buffer;
}
///
/// ulong变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( ulong value )
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// ulong数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( ulong[] values )
{
if (values == null) return null;
byte[] buffer = new byte[values.Length * 8];
for (int i = 0; i < values.Length; i++)
{
GetBytes(values[i]).CopyTo( buffer, 8 * i );
}
return buffer;
}
///
/// float变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( float value )
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// float数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( float[] values )
{
if (values == null) return null;
byte[] buffer = new byte[values.Length * 4];
for (int i = 0; i < values.Length; i++)
{
GetBytes(values[i]).CopyTo( buffer, 4 * i );
}
return buffer;
}
///
/// double变量转化缓存数据
///
/// 等待转化的数据
/// buffer数据
public byte[] GetBytes( double value )
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return bytes;
}
///
/// double数组变量转化缓存数据
///
/// 等待转化的数组
/// buffer数据
public byte[] GetBytes( double[] values )
{
if (values == null) return null;
byte[] buffer = new byte[values.Length * 8];
for (int i = 0; i < values.Length; i++)
{
GetBytes( values[i] ).CopyTo( buffer, 8 * i );
}
return buffer;
}
///
/// 使用指定的编码字符串转化缓存数据
///
/// 等待转化的数据
/// 字符串的编码方式
/// buffer数据
public byte[] GetBytes( string value, Encoding encoding )
{
return encoding.GetBytes( value );
}
#endregion
}
}