/// <summary> /// 返回由64位有符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static unsafe byte[] ToBytes(long value, Endians endian) { byte[] bytes = new byte[8]; fixed (byte* pbyte = &bytes[0]) { if (endian == Endians.Little) { *pbyte = (byte)(value); *(pbyte + 1) = (byte)(value >> 8); *(pbyte + 2) = (byte)(value >> 16); *(pbyte + 3) = (byte)(value >> 24); *(pbyte + 4) = (byte)(value >> 32); *(pbyte + 5) = (byte)(value >> 40); *(pbyte + 6) = (byte)(value >> 48); *(pbyte + 7) = (byte)(value >> 56); } else { *(pbyte + 7) = (byte)(value); *(pbyte + 6) = (byte)(value >> 8); *(pbyte + 5) = (byte)(value >> 16); *(pbyte + 4) = (byte)(value >> 24); *(pbyte + 3) = (byte)(value >> 32); *(pbyte + 2) = (byte)(value >> 40); *(pbyte + 1) = (byte)(value >> 48); *pbyte = (byte)(value >> 56); } } return bytes; }
/// <summary> /// 返回由字节数组中指定位置的四个字节转换来的32位有符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public unsafe static int ToInt32(byte[] bytes, int startIndex, Endians endian) { fixed (byte* pbyte = &bytes[startIndex]) { if (endian == Endians.Little) { return (*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24); } else { return (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3)); } } }
/// <summary> /// 返回由字节数组中指定位置的8个字节转换来的64位有符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static unsafe long ToInt64(byte[] bytes, int startIndex, Endians endian) { fixed (byte* pbyte = &bytes[startIndex]) { if (endian == Endians.Little) { int i1 = (*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24); int i2 = (*(pbyte + 4)) | (*(pbyte + 5) << 8) | (*(pbyte + 6) << 16) | (*(pbyte + 7) << 24); return (uint)i1 | ((long)i2 << 32); } else { int i1 = (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3)); int i2 = (*(pbyte + 4) << 24) | (*(pbyte + 5) << 16) | (*(pbyte + 6) << 8) | (*(pbyte + 7)); return (uint)i2 | ((long)i1 << 32); } } }
/// <summary> /// 返回由32位有符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static unsafe byte[] ToBytes(int value, Endians endian) { byte[] bytes = new byte[4]; fixed (byte* pbyte = &bytes[0]) { if (endian == Endians.Little) { *pbyte = (byte)(value); *(pbyte + 1) = (byte)(value >> 8); *(pbyte + 2) = (byte)(value >> 16); *(pbyte + 3) = (byte)(value >> 24); } else { *(pbyte + 3) = (byte)(value); *(pbyte + 2) = (byte)(value >> 8); *(pbyte + 1) = (byte)(value >> 16); *pbyte = (byte)(value >> 24); } } return bytes; }
/// <summary> /// 返回由32位有符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public unsafe static byte[] ToBytes(int value, Endians endian) { byte[] bytes = new byte[4]; fixed(byte *pbyte = &bytes[0]) { if (endian == Endians.Little) { *pbyte = (byte)(value); *(pbyte + 1) = (byte)(value >> 8); *(pbyte + 2) = (byte)(value >> 16); *(pbyte + 3) = (byte)(value >> 24); } else { *(pbyte + 3) = (byte)(value); *(pbyte + 2) = (byte)(value >> 8); *(pbyte + 1) = (byte)(value >> 16); *pbyte = (byte)(value >> 24); } } return(bytes); }
/// <summary> /// 返回由字节数组中指定位置的8个字节转换来的64位有符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static long ToInt64(byte[] bytes, int startIndex, Endians endian) { long result = 0L; if (endian == Endians.Little) { int i1 = (int)(bytes[startIndex] | bytes[startIndex + 1] << 8 | bytes[startIndex + 2] << 16 | bytes[startIndex + 3] << 24); int i2 = (int)(bytes[startIndex + 4] | bytes[startIndex + 5] << 8 | bytes[startIndex + 6] << 16 | bytes[startIndex + 7] << 24); result = (uint)i1 | ((long)i2 << 32); } else { int i1 = (int)(bytes[startIndex] << 24 | bytes[startIndex + 1] << 16 | bytes[startIndex + 2] << 8 | bytes[startIndex + 3]); int i2 = (int)(bytes[startIndex + 4] << 24 | bytes[startIndex + 5] << 16 | bytes[startIndex + 6] << 8 | bytes[startIndex + 7]); result = (uint)i2 | ((long)i1 << 32); } return(result); }
/// <summary> /// 从流中读取2个字节,并将流内的位置向前推进2个字节, /// 返回其Int16表示类型 /// </summary> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public short ReadInt16(Endians endian) { var range = this.ReadArraySegment(sizeof(short)); return(ByteConverter.ToInt16(range.Array, range.Offset, endian)); }
/// <summary> /// 返回由字节数组中指定位置的8个字节转换来的64位无符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static ulong ToUInt64(byte[] bytes, int startIndex, Endians endian) { return((ulong)ToInt64(bytes, startIndex, endian)); }
/// <summary> /// 内存数据流 /// </summary> /// <param name="endians">节存储次序</param> public NsStream(Endians endians) { SyncRoot = new object(); Endian = endians; }
/// <summary> /// 读取指定位置8个字节,返回其UInt64表示类型 /// </summary> /// <param name="index">字节所在索引</param> /// <param name="endian">高低位</param> /// <returns></returns> public ulong ToUInt64(int index, Endians endian) { return(ByteConverter.ToUInt64(Source, index, endian)); }
/// <summary> /// 读取指定位置2个字节,返回其Int16表示类型 /// </summary> /// <param name="index">字节所在索引</param> /// <param name="endian">高低位</param> /// <returns></returns> public int ToInt16(int index, Endians endian) { return(ByteConverter.ToInt16(Source, index, endian)); }
/// <summary> /// 返回由字节数组中指定位置的8个字节转换来的64位无符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static ulong ToUInt64(byte[] bytes, int startIndex, Endians endian) { return (ulong)ToInt64(bytes, startIndex, endian); }
/// <summary> /// 提供二进制数据读取和操作支持 /// </summary> /// <param name="endian">字节存储次序</param> public ByteBuilder(Endians endian) { Endian = endian; }
/// <summary> /// 内存数据流 /// </summary> /// <param name="endians">节存储次序</param> public NsStream(Endians endians) { this.SyncRoot = new object(); this.Endian = endians; }
public void setValue(Enum item, Single value, Endians endian) { setValue(item, (Double)value, endian); }
public void setValue(Enum item, Byte value, Endians endian) { setValue(item, (Int64)value, endian); }
/// <summary> /// 해당되는 unit의 값을 가져옴. 알맞는 타입으로 캐스팅해서 쓰면 된다. /// </summary> /// <param name="item"></param> /// <param name="endian"></param> /// <returns></returns> public Object getValue(Enum item, Endians endian = Endians.Little) { int startOffset = 0; int byteSize = 0; int bitStart = -1; int bitSize = -1; int returnSize = Marshal.SizeOf(_returnType[item]); if (_startOffsetInBuffer.ContainsKey(item))//버퍼상에 실제 할당되는 unit { byteSize = _sizeOfItemInBuffer[item]; startOffset = _startOffsetInBuffer[item]; if (byteSize == 1) { Byte[] aByte = new Byte[1]; Buffer.BlockCopy(_initBuffer, startOffset, aByte, 0, 1); return(getWithLSB(item, aByte[0])); } else if (byteSize == 2) { return(getWithLSB(item, BitConverter.ToInt16(_initBuffer, startOffset))); } else if (byteSize == 4) { return(getWithLSB(item, BitConverter.ToInt32(_initBuffer, startOffset))); } else //if (byteSize == 8) { return(getWithLSB(item, BitConverter.ToInt64(_initBuffer, startOffset))); } } else if (_bitIndex.ContainsKey(item)) //bit 분리된 unit { Enum parent = _parentUnitOfBitItem[item]; byteSize = _sizeOfItemInBuffer[parent]; startOffset = _startOffsetInBuffer[parent]; bitStart = _bitIndex[item]; bitSize = _bitSize[item]; if (byteSize == 1) { return(getWithLSB(item, trimBit(_initBuffer[startOffset], bitStart, bitSize))); } else if (byteSize == 2) { return(getWithLSB(item, trimBit(BitConverter.ToInt16(_initBuffer, startOffset), bitStart, bitSize))); } else if (byteSize == 4) { return(getWithLSB(item, trimBit(BitConverter.ToInt32(_initBuffer, startOffset), bitStart, bitSize))); } else// if (byteSize == 8) { if (bitSize > 32) { return(getWithLSB(item, trimBit(BitConverter.ToInt64(_initBuffer, startOffset), bitStart, bitSize))); } else { return(getWithLSB(item, trimBit(BitConverter.ToInt32(_initBuffer, startOffset), bitStart, bitSize))); } } } else //merged unit { List <Enum> mergingUnits = _mergedItems[item]; int unitSize = _sizeOfItemInBuffer[mergingUnits[0]]; //unit 하나의 크기 byteSize = unitSize * mergingUnits.Count; //전체크기 startOffset = _startOffsetInBuffer[mergingUnits[0]]; if (_mswInMergedItem[item] == MswPos.Before && endian == Endians.Little) { if (byteSize == 2) { return(getWithLSB(item, UnitsSwapTo <Int16>(startOffset, startOffset + 1))); } else if (byteSize == 4) { return(getWithLSB(item, UnitsSwapTo <Int32>(startOffset, startOffset + 1))); } else if (byteSize == 8) { return(getWithLSB(item, UnitsSwapTo <Int64>(startOffset, startOffset + 1))); } else //지원하지 않음. 1은 에러. { throw new Exception("지원하지 않는크기. mergedUnit은 최소한 2이상이고 8이하여야 함"); } } else { if (byteSize == 2) { return(getWithLSB(item, UnitsTo <Int16>(startOffset, startOffset + 1))); } else if (byteSize == 4) { return(getWithLSB(item, UnitsTo <Int16>(startOffset, startOffset + 1))); } else if (byteSize == 8) { return(getWithLSB(item, UnitsTo <Int16>(startOffset, startOffset + 1))); } else {//지원하지 않음. 1은 에러. throw new Exception("지원하지 않는크기. mergedUnit은 최소한 2이상이고 8이하여야 함"); } } } }
/// <summary> /// 从流中读取8个字节,并将流内的位置向前推进8个字节, /// 返回其UInt64表示类型 /// </summary> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public ulong ReadUInt64(Endians endian) { var range = this.ReadArraySegment(sizeof(ulong)); return(ByteConverter.ToUInt64(range.Array, range.Offset, endian)); }
/// <summary> /// 读取指定位置8个字节,返回其Int64表示类型 /// </summary> /// <param name="index">字节所在索引</param> /// <param name="endian">高低位</param> /// <returns></returns> public long ToInt64(int index, Endians endian) { return(ByteConverter.ToInt64(this.Source, index, endian)); }
/// <summary> /// 提供二进制数据读取和操作支持 /// </summary> /// <param name="endian">字节存储次序</param> public ByteBuilder(Endians endian) { this.Endian = endian; }
/// <summary> /// 返回由64位无符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static byte[] ToBytes(ulong value, Endians endian) { return ToBytes((long)value, endian); }
/// <summary> /// 返回由16位有符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public unsafe static byte[] ToBytes(short value, Endians endian) { byte[] bytes = new byte[2]; fixed (byte* pbyte = &bytes[0]) { if (endian == Endians.Little) { *pbyte = (byte)(value); *(pbyte + 1) = (byte)(value >> 8); } else { *(pbyte + 1) = (byte)(value); *pbyte = (byte)(value >> 8); } } return bytes; }
/// <summary> /// 返回由32位无符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static byte[] ToBytes(uint value, Endians endian) { return ToBytes((int)value, endian); }
/// <summary> /// 返回由字节数组中指定位置的四个字节转换来的16位无符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static ushort ToUInt16(byte[] bytes, int startIndex, Endians endian) { return((ushort)ToInt16(bytes, startIndex, endian)); }
/// <summary> /// 读取指定位置4个字节,返回其UInt32表示类型 /// </summary> /// <param name="index">字节所在索引</param> /// <param name="endian">高低位</param> /// <returns></returns> public uint ToUInt32(int index, Endians endian) { return(ByteConverter.ToUInt32(Source, index, endian)); }
/// <summary> /// 返回由32位无符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static byte[] ToBytes(uint value, Endians endian) { return(ToBytes((int)value, endian)); }
/// <summary> /// 将32位整数转换为byte数组再添加 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> public void Add(int value, Endians endian) { var bytes = ByteConverter.ToBytes(value, endian); Add(bytes); }
/// <summary> /// 将16位整数转换为byte数组再添加 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> public void Add(ushort value, Endians endian) { var bytes = ByteConverter.ToBytes(value, endian); this.Add(bytes); }
/// <summary> /// 返回由64位无符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static byte[] ToBytes(ulong value, Endians endian) { return(ToBytes((long)value, endian)); }
/// <summary> /// 返回由16位无符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static byte[] ToBytes(ushort value, Endians endian) { return ToBytes((short)value, endian); }
/// <summary> /// 返回由16位无符号整数转换为的字节数组 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> /// <returns></returns> public static byte[] ToBytes(ushort value, Endians endian) { return(ToBytes((short)value, endian)); }
/// <summary> /// 返回由字节数组中指定位置的四个字节转换来的16位有符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static unsafe short ToInt16(byte[] bytes, int startIndex, Endians endian) { fixed (byte* pbyte = &bytes[startIndex]) { if (endian == Endians.Little) { return (short)((*pbyte) | (*(pbyte + 1) << 8)); } else { return (short)((*pbyte << 8) | (*(pbyte + 1))); } } }
/// <summary> /// 返回由字节数组中指定位置的四个字节转换来的32位无符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static uint ToUInt32(byte[] bytes, int startIndex, Endians endian) { return((uint)ToInt32(bytes, startIndex, endian)); }
/// <summary> /// 返回由字节数组中指定位置的四个字节转换来的16位无符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static ushort ToUInt16(byte[] bytes, int startIndex, Endians endian) { return (ushort)ToInt16(bytes, startIndex, endian); }
/// <summary> /// 从流中读取4个字节,并将流内的位置向前推进4个字节, /// 返回其UInt32表示类型 /// </summary> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public uint ReadUInt32(Endians endian) { var range = this.ReadArraySegment(sizeof(uint)); return(ByteConverter.ToUInt32(range.Array, range.Offset, endian)); }
/// <summary> /// 返回由字节数组中指定位置的四个字节转换来的32位无符号整数 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="startIndex">位置</param> /// <param name="endian">高低位</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public static uint ToUInt32(byte[] bytes, int startIndex, Endians endian) { return (uint)ToInt32(bytes, startIndex, endian); }
/// <summary> /// 读取指定位置4个字节,返回其Int32表示类型 /// </summary> /// <param name="index">字节所在索引</param> /// <param name="endian">高低位</param> /// <returns></returns> public int ToInt32(int index, Endians endian) { return(ByteConverter.ToInt32(this.Source, index, endian)); }