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