/// <summary> /// Creates an index buffer stream. /// </summary> /// <param name="stream">The base stream to use. It must point to the beginning of the index buffer.</param> /// <param name="endianFormat">The endian format of the index buffer.</param> public IndexBufferStream(Stream stream, EndianFormat endianFormat = EndianFormat.LittleEndian) { _stream = stream; _reader = new EndianReader(_stream, endianFormat); _writer = new EndianWriter(_stream, endianFormat); _baseOffset = _stream.Position; }
public static void Write(this BinaryWriter writer, Rectangle2d value, EndianFormat format = EndianFormat.LittleEndian) { writer.Write(value.Top, format); writer.Write(value.Left, format); writer.Write(value.Bottom, format); writer.Write(value.Right, format); }
public static string ToBase36string(this byte[] bytes, EndianFormat bytesEndian = EndianFormat.Little, bool includeProceedingZeros = true) { var base36_no_zeros = new RadixEncoding(k_base36_digits, bytesEndian, includeProceedingZeros); return(base36_no_zeros.Encode(bytes)); }
public void Shell_PlatformApiTest() { Assert.AreEqual(6, Processor.BitCount, "Expected Processor bit count size has changed. Was this intentional?"); Assert.AreEqual(6 + 4, Platform.BitCount, "Expected Platform bit count size has changed. Was this intentional?"); const InstructionSet k_xenon_instruction_set = InstructionSet.PPC; const ProcessorSize k_xenon_processor_size = ProcessorSize.x32; const EndianFormat k_xenon_byte_order = EndianFormat.Big; var xenon_processor = new Processor(k_xenon_processor_size, k_xenon_byte_order, k_xenon_instruction_set); Assert.AreEqual(Processor.PowerPcXenon, xenon_processor); var xenon_platform = new Platform(PlatformType.Xbox, xenon_processor); Assert.AreEqual(Platform.Xbox360, xenon_platform); Assert.AreEqual(Processor.PowerPcXenon, xenon_platform.ProcessorType); Assert.AreEqual(k_xenon_instruction_set, xenon_platform.ProcessorType.InstructionSet); Assert.AreEqual(k_xenon_processor_size, xenon_platform.ProcessorType.ProcessorSize); Assert.AreEqual(k_xenon_byte_order, xenon_platform.ProcessorType.ByteOrder); Assert.IsTrue(Platform.Win32.CompareTo(Platform.Win64) < 0, "ProcessorSize's x32 member should come before x64, yet Win32 is not less-than Win64"); }
public void Read(EndianReader reader) { EndianFormat = DetectEndianFormat(reader); MapVersion = GetMapFileVersion(reader); Version = DetectCacheVersion(reader); var deserializer = new TagDeserializer(Version); reader.SeekTo(0); var dataContext = new DataSerializationContext(reader); Header = (CacheFileHeader)deserializer.Deserialize(dataContext, typeof(CacheFileHeader)); // temporary code until map file format cleanup if (MapVersion == CacheFileVersion.HaloOnline) { var mapFileHeaderSize = (int)TagStructure.GetTagStructureInfo(typeof(CacheFileHeader), Version).TotalSize; // Seek to the blf reader.SeekTo(mapFileHeaderSize); // Read blf MapFileBlf = new Blf(Version); if (!MapFileBlf.Read(reader)) { MapFileBlf = null; } } }
/// <summary> /// Converts the give byte into a guid. The guid can be stored in a big endian or little /// endian format. /// </summary> /// <param name="values">The byte array</param> /// <param name="startOffset">Starting offset of where to start reading the guid</param> /// <param name="endian">How to interpret the bytes (big or little endian)</param> /// <returns>The byte array as a guid</returns> /// <exception cref="ArgumentOutOfRangeException">If there aren't enough bytes in the guid</exception> public static Guid ToGuid(this byte[] values, int startOffset = 0, EndianFormat endian = EndianFormat.Big) { var neededSize = startOffset + GuidSize; if (neededSize > values.Length) { throw new ArgumentOutOfRangeException(nameof(values), values.Length, $"Array too small to read full GUID with given startOffset"); } var index = startOffset; var a = values.ToInt32(index, endian); index += sizeof(Int32); var b = values.ToInt16(index, endian); index += sizeof(Int16); var c = values.ToInt16(index, endian); index += sizeof(Int16); var byteArray = new byte[8]; for (var byteIndex = 0; byteIndex < 8; byteIndex += 1) { byteArray[byteIndex] = values[index++]; } var guid = new Guid(a, b, c, byteArray); return(guid); }
/// <summary> /// Convert to specified Cache Version. /// </summary> /// <param name="targetVersion"></param> public void ConvertBlf(CacheVersion targetVersion) { switch (Version) { case CacheVersion.Halo3Retail: case CacheVersion.Halo3Beta: switch (targetVersion) { case CacheVersion.Halo3ODST: case CacheVersion.HaloOnline106708: ConvertHalo3ToODSTScenarioChunk(); Version = targetVersion; if (targetVersion == CacheVersion.HaloOnline106708) { Format = EndianFormat.LittleEndian; } break; default: throw new NotImplementedException($"Conversion from Halo 3 to {targetVersion.ToString()} not supported"); } break; case CacheVersion.Halo3ODST: case CacheVersion.HaloOnline106708: if (targetVersion == CacheVersion.HaloOnline106708) { Format = EndianFormat.LittleEndian; } return; default: throw new NotImplementedException($"Conversion from {Version.ToString()} to {targetVersion.ToString()} not supported"); } }
/// <summary> /// Verifies if the stream points to a valid blf start chunk and set the endian format. /// </summary> /// <param name="reader"></param> /// <returns></returns> private bool IsValid(EndianReader reader) { var position = reader.Position; try { Format = FindChunkEndianFormat(reader); reader.Format = Format; } catch (Exception e) { Console.WriteLine($"BLF file is invalid: {e.Message}"); return(false); } var deserializer = new TagDeserializer(Version); var dataContext = new DataSerializationContext(reader); var header = (BlfChunkHeader)deserializer.Deserialize(dataContext, typeof(BlfChunkHeader)); reader.SeekTo(position); if (header.Signature == "_blf") { return(true); } else { return(false); } }
private void but_open_Click(object sender, EventArgs e) { try { if (txt_content.Text.Contains("小技巧")) { txt_content.Text = string.Empty; } client?.Close(); EndianFormat format = EndianFormat.ABCD; switch (comboBox1.SelectedIndex) { case 0: format = EndianFormat.ABCD; break; case 1: format = EndianFormat.BADC; break; case 2: format = EndianFormat.CDAB; break; case 3: format = EndianFormat.DCBA; break; } if (chb_rtudata.Checked) { client = new ModbusTcpRtuClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()), format: format); } else { client = new ModbusTcpClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()), format: format); } var result = client.Open(); if (result.IsSucceed) { but_read.Enabled = true; but_write.Enabled = true; but_open.Enabled = false; but_close.Enabled = true; but_sendData.Enabled = true; AppendText($"连接成功"); ControlEnabledFalse(); } else { MessageBox.Show($"连接失败:{result.Err}"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Reads a UInt16 value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public ushort ReadUInt16(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) { return(base.ReadUInt16()); } byte[] bytes = base.ReadBytes(2); Array.Reverse(bytes); return(BitConverter.ToUInt16(bytes, 0)); }
/// <summary> /// Reads a Double value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public double ReadDouble(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) { return(base.ReadDouble()); } byte[] bytes = base.ReadBytes(8); Array.Reverse(bytes); return(BitConverter.ToDouble(bytes, 0)); }
/// <summary> /// Reads an Int32 value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public int ReadInt32(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) { return(base.ReadInt32()); } byte[] bytes = base.ReadBytes(4); Array.Reverse(bytes); return(BitConverter.ToInt32(bytes, 0)); }
/// <summary> /// Reads a Single value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public float ReadSingle(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) { return(base.ReadSingle()); } byte[] bytes = base.ReadBytes(4); Array.Reverse(bytes); return(BitConverter.ToSingle(bytes, 0)); }
/// <summary> /// Reads a UInt64 value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public ulong ReadUInt64(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) { return(base.ReadUInt64()); } byte[] bytes = base.ReadBytes(8); Array.Reverse(bytes); return(BitConverter.ToUInt64(bytes, 0)); }
public static void Write(this BinaryWriter writer, ushort value, EndianFormat format) { if (format == EndianFormat.BigEndian) { writer.Write(BitConverter.GetBytes(value).Reverse().ToArray()); } else { writer.Write(value); } }
/// <summary>Create a radix encoder using the given characters as the digits in the radix</summary> /// <param name="digits">Digits to use for the radix-encoded string</param> /// <param name="bytesEndian">Endian ordering of bytes input to Encode and output by Decode</param> /// <param name="includeProceedingZeros">True if we want ending zero bytes to be encoded</param> public RadixEncoding(string digits, EndianFormat bytesEndian = EndianFormat.Little, bool includeProceedingZeros = false) { Contract.Requires<ArgumentNullException>(digits != null); var radix = digits.Length; _digits = digits; _bitsPerDigit = Math.Log(radix, 2); _radixBig = new BigInteger(radix); Endian = bytesEndian; IncludeProceedingZeros = includeProceedingZeros; }
/// <summary>Create a radix encoder using the given characters as the digits in the radix</summary> /// <param name="digits">Digits to use for the radix-encoded string</param> /// <param name="bytesEndian">Endian ordering of bytes input to Encode and output by Decode</param> /// <param name="includeProceedingZeros">True if we want ending zero bytes to be encoded</param> public BaseEncoding(string digits, EndianFormat bytesEndian = EndianFormat.Little, bool includeProceedingZeros = false) { //Contract.Requires<ArgumentNullException>(digits != null); int radix = digits.Length; kDigits = digits; kBitsPerDigit = System.Math.Log(radix, 2); kRadixBig = new BigInteger(radix); kEndian = bytesEndian; kIncludeProceedingZeros = includeProceedingZeros; }
/// <summary> /// Writes a Double value in the specified EndianFormat. /// </summary> /// <param name="value">The value to write to the stream.</param> /// <param name="Type">The EndianFormat to write the value in.</param> public void Write(double value, EndianFormat Type) { byte[] bits = BitConverter.GetBytes(value); if (Type == EndianFormat.BigEndian) { Array.Reverse(bits); } base.Write(bits); }
public static byte[] ConvertBase36StringToBytes(string base36string, EndianFormat bytesEndian = EndianFormat.Little, bool includeProceedingZeros = true) { var base36_no_zeros = new RadixEncoding(k_base36_digits, bytesEndian, includeProceedingZeros); var bytes = new List <byte>(base36_no_zeros.Decode(base36string)); //while (bytes[bytes.Count - 1] == 0) //{ // bytes.RemoveAt(bytes.Count - 1); //} return(bytes.ToArray()); }
private void but_open_Click(object sender, EventArgs e) { try { var PortName = cb_portNameSend.Text.ToString(); var BaudRate = int.Parse(cb_baudRate.Text.ToString()); var DataBits = int.Parse(txt_dataBit.Text.ToString()); var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString()); var parity = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even); client?.Close(); EndianFormat format = EndianFormat.ABCD; switch (comboBox1.SelectedIndex) { case 0: format = EndianFormat.ABCD; break; case 1: format = EndianFormat.BADC; break; case 2: format = EndianFormat.CDAB; break; case 3: format = EndianFormat.DCBA; break; } client = new ModbusAsciiClient(PortName, BaudRate, DataBits, StopBits, parity, format: format); var result = client.Open(); if (result.IsSucceed) { but_open.Enabled = false; cb_portNameSend.Enabled = false; but_read.Enabled = true; but_write.Enabled = true; but_open.Enabled = false; but_close.Enabled = true; but_sendData.Enabled = true; AppendText("连接成功"); ControlEnabledFalse(); } else { AppendText($"连接失败:{result.Err}"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public RadixEncoding(string digits = _charset, EndianFormat bytesEndian = EndianFormat.Little, bool includeProceedingZeros = false) { Ensure.Requires <ArgumentNullException>(digits != null); if (digits != null) { var radix = digits.Length; kDigits = digits; kBitsPerDigit = Math.Log(radix, 2); kRadixBig = new BigInteger(radix); } kEndian = bytesEndian; kIncludeProceedingZeros = includeProceedingZeros; }
/// <summary> /// Returns the next UInt16 and does not advance the stream position. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public ushort PeekUInt16(EndianFormat Type) { ushort val; if (Type == EndianFormat.LittleEndian) val = base.ReadUInt16(); else { byte[] bytes = base.ReadBytes(2); Array.Reverse(bytes); val = BitConverter.ToUInt16(bytes, 0); } Skip(-2); return val; }
private byte[] SortExpressionDataArray(EndianFormat format, byte[] data, int dataLength) { if (format == EndianFormat.BigEndian) { byte[] newData = new byte[dataLength]; // reverse the data array, but only to the specified length for (int i = 0; i < dataLength; i++) { newData[i] = data[(dataLength - 1) - i]; } return(newData); } return(data); }
private void ReadBlocks(EndianReader reader, long fileSize) { EndianFormat defaultEndian = reader.Format; long startOffset = reader.Position; long endOffset = startOffset + fileSize; long offset = startOffset; while (offset < endOffset) { // Read the block header // The magic value is *always* big-endian, so switch to it temporarily reader.Format = EndianFormat.Big; int blockMagic = reader.ReadInt32(); reader.Format = defaultEndian; int blockSize = reader.ReadInt32(); offset += 8; // Process the block based upon its magic value switch (blockMagic) { case BlockMagic.BKHD: ReadHeader(reader); break; case BlockMagic.DIDX: ReadFiles(reader, blockSize); break; case BlockMagic.DATA: // Just store the offset of this block's contents to the DataOffset field DataOffset = (int)(offset - startOffset); break; case BlockMagic.HIRC: ReadObjects(reader); break; } // Skip to the next block offset += blockSize; reader.SeekTo(offset); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="portName">COM端口名称</param> /// <param name="baudRate">波特率</param> /// <param name="dataBits">数据位</param> /// <param name="stopBits">停止位</param> /// <param name="parity">奇偶校验</param> /// <param name="timeout">超时时间(毫秒)</param> /// <param name="format">大小端设置</param> public ModbusSerialBase(string portName, int baudRate, int dataBits, StopBits stopBits, Parity parity, int timeout = 1500, EndianFormat format = EndianFormat.ABCD) { if (serialPort == null) { serialPort = new SerialPort(); } serialPort.PortName = portName; serialPort.BaudRate = baudRate; serialPort.DataBits = dataBits; serialPort.StopBits = stopBits; serialPort.Encoding = Encoding.ASCII; serialPort.Parity = parity; serialPort.ReadTimeout = timeout; serialPort.WriteTimeout = timeout; this.format = format; }
/// <summary> /// Returns the next UInt16 and does not advance the stream position. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public ushort PeekUInt16(EndianFormat Type) { ushort val; if (Type == EndianFormat.LittleEndian) { val = base.ReadUInt16(); } else { byte[] bytes = base.ReadBytes(2); Array.Reverse(bytes); val = BitConverter.ToUInt16(bytes, 0); } Skip(-2); return(val); }
public static TagFunction ConvertTagFunction(EndianFormat format, TagFunction function) { if (function == null || function.Data == null) { return(null); } if (function.Data.Length == 0) { return(function); } var result = new byte[function.Data.Length]; using (var inputReader = new EndianReader(new MemoryStream(function.Data), format)) using (var outputWriter = new EndianWriter(new MemoryStream(result), EndianFormat.LittleEndian)) { TagFunctionHeader header = new TagFunctionHeader(); header.Read(inputReader); header.Write(outputWriter); var opcode = header.Opcode; ParseTagFunctionData(header.Opcode, inputReader, outputWriter); if (header.Interpolated()) { ParseTagFunctionData(header.Opcode, inputReader, outputWriter); } // If any tag function has remaining data, just endian swap it. It is a very rare occurence, not handled by the HO parser. That piece of the function is most likely ignored at runtime. while (!inputReader.EOF) { float temp = inputReader.ReadSingle(); outputWriter.Write(temp); } function.Data = result; } return(function); }
/// <summary> /// Reads an Int32 value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public int ReadInt32(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) return base.ReadInt32(); byte[] bytes = base.ReadBytes(4); Array.Reverse(bytes); return BitConverter.ToInt32(bytes, 0); }
/// <summary> /// 构造函数 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="timeout">超时时间(毫秒)</param> /// <param name="format">大小端设置</param> public ModbusRtuOverTcpClient(string ip, int port, int timeout = 1500, EndianFormat format = EndianFormat.ABCD) { this.timeout = timeout; this.ipAndPoint = new IPEndPoint(IPAddress.Parse(ip), port); this.format = format; }
/// <summary> /// Creates a new instance of the EndianReader class. /// </summary> /// <param name="Stream">The Stream to read from.</param> /// <param name="Type">The default EndianFormat the EndianReader will use.</param> public EndianReader(Stream Stream, EndianFormat Type) : base(Stream) { EndianType = Type; StreamOrigin = 0; }
public static void Write(this BinaryWriter writer, DatumIndex value, EndianFormat format = EndianFormat.LittleEndian) { writer.Write(value.Value, format); }
/// <summary> /// Reads a UInt64 value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public ulong ReadUInt64(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) return base.ReadUInt64(); byte[] bytes = base.ReadBytes(8); Array.Reverse(bytes); return BitConverter.ToUInt64(bytes, 0); }
/// <summary> /// Creates a new instance of the EndianReader class. /// </summary> /// <param name="Stream">The Stream to read from.</param> /// <param name="leaveOpen">Whether to leave the underlying stream open.</param> /// <param name="Type">The default EndianFormat the EndianReader will use.</param> public EndianReader(Stream Stream, bool leaveOpen, EndianFormat Type = EndianFormat.LittleEndian) : base(Stream, Encoding.ASCII, leaveOpen) { Format = Type; StreamOrigin = 0; }
public void Write(double value, EndianFormat format) => Write(format == Big ? GetBytes(value).Reverse().ToArray() : GetBytes(value));
public double ReadDouble(EndianFormat format) => format == Big ? ToDouble(ReadBytes(8).Reverse().ToArray(), 0) : base.ReadDouble();
public float ReadSingle(EndianFormat format) => format == Big ? ToSingle(ReadBytes(4).Reverse().ToArray(), 0) : base.ReadSingle();
public ulong ReadUInt64(EndianFormat format) => format == Big ? ToUInt64(ReadBytes(8).Reverse().ToArray(), 0) : base.ReadUInt64();
public uint ReadUInt32(EndianFormat format) => format == Big ? ToUInt32(ReadBytes(4).Reverse().ToArray(), 0) : base.ReadUInt32();
public ushort ReadUInt16(EndianFormat format) => format == Big ? ToUInt16(ReadBytes(2).Reverse().ToArray(), 0) : base.ReadUInt16();
public EndianReader(Stream stream, EndianFormat format) : base(stream) { Format = format; Origin = 0; }
/// <summary> /// Reads a UInt16 value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public ushort ReadUInt16(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) return base.ReadUInt16(); byte[] bytes = base.ReadBytes(2); Array.Reverse(bytes); return BitConverter.ToUInt16(bytes, 0); }
/// <summary> /// Reads a Double value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public double ReadDouble(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) return base.ReadDouble(); byte[] bytes = base.ReadBytes(8); Array.Reverse(bytes); return BitConverter.ToDouble(bytes, 0); }
public static void Write(this BinaryWriter writer, Point2d value, EndianFormat format = EndianFormat.LittleEndian) { writer.Write(value.X, format); writer.Write(value.Y, format); }
public EndianWriter(Stream stream, EndianFormat format) : base(stream) { Format = format; }
/// <summary> /// Reads a Single value in the specified EndianFormat. /// </summary> /// <param name="Type">The EndianFormat of the value.</param> /// <returns></returns> public float ReadSingle(EndianFormat Type) { if (Type == EndianFormat.LittleEndian) return base.ReadSingle(); byte[] bytes = base.ReadBytes(4); Array.Reverse(bytes); return BitConverter.ToSingle(bytes, 0); }
/// <summary> /// Creates a new instance of the EndianWriter class. /// </summary> /// <param name="Stream">The Stream to write to.</param> /// <param name="Type">The default EndianFormat the EndianWriter will use.</param> public EndianWriter(Stream Stream, EndianFormat Type) : base(Stream) { EndianType = Type; }
/// <summary> /// Writes a Double value in the specified EndianFormat. /// </summary> /// <param name="value">The value to write to the stream.</param> /// <param name="Type">The EndianFormat to write the value in.</param> public void Write(double value, EndianFormat Type) { byte[] bits = BitConverter.GetBytes(value); if (Type == EndianFormat.BigEndian) Array.Reverse(bits); base.Write(bits); }