public FlvReader CreateFlvReaderWithTag() { var bytes = Enumerable.Repeat((byte)0, 18).ToList(); var payloadSize = new UInt24(10); bytes.InsertRange(1, payloadSize.ToByteArray(Endianness.BigEndian)); var flvReader = CreateFlvReader(bytes.ToArray()); flvReader.MoveToBeforeTagState(); return flvReader; }
public void Usage() { UInt24 int24 = new UInt24(10); Assert.That(int24.Value, Is.EqualTo(10)); Int24 secInt24 = new Int24(); Assert.That(secInt24.Value, Is.EqualTo(0)); secInt24.Value = 5; Assert.That(secInt24.Value, Is.EqualTo(5)); }
internal override IpV6AccessNetworkIdentifierSubOption CreateInstance(DataSegment data) { if (data.Length != OptionDataLength) { return(null); } UInt24 latitudeDegrees = data.ReadUInt24(Offset.LatitudeDegrees, Endianity.Big); UInt24 longitudeDegrees = data.ReadUInt24(Offset.LongitudeDegrees, Endianity.Big); return(new IpV6AccessNetworkIdentifierSubOptionGeoLocation(latitudeDegrees, longitudeDegrees)); }
public void Usage() { UInt24 int24 = new UInt24 { Value = 32647 }; byte[] byteArray = int24.ToByteArray(); Assert.That(byteArray[0], Is.EqualTo(135)); Assert.That(byteArray[1], Is.EqualTo(127)); Assert.That(byteArray[2], Is.EqualTo(0)); }
public FlvReader CreateFlvReaderWithTag() { var bytes = Enumerable.Repeat((byte)0, 18).ToList(); var payloadSize = new UInt24(10); bytes.InsertRange(1, payloadSize.ToByteArray(Endianness.BigEndian)); var flvReader = CreateFlvReader(bytes.ToArray()); flvReader.MoveToBeforeTagState(); return(flvReader); }
/// <summary> /// Attempts to convert a radix value to an integer value. /// </summary> /// <param name="radixValue">Radix value to convert.</param> /// <param name="value">Decoded integer value.</param> /// <returns><c>true</c> if decode succeeds; otherwise, <c>false</c>.</returns> public bool TryDecode(string radixValue, out UInt24 value) { try { value = m_uint24.Decode(radixValue); return(true); } catch { value = 0; return(false); } }
// Token: 0x060004CF RID: 1231 RVA: 0x0000F6C8 File Offset: 0x0000D8C8 public UInt24[] Read24s(int count) { if (base.IsDisposed) { throw new ObjectDisposedException("ABinaryReader"); } UInt24[] array = new UInt24[count]; base.FillBuffer(3, count); for (int i = 0; i < count; i++) { array[i] = (UInt24)((int)base.Buffer[3 * i + 2] << 16 | (int)base.Buffer[3 * i + 1] << 8 | (int)base.Buffer[3 * i]); } return(array); }
public void Should_SerializeUInt24() { UInt24 value = UInt24.MaxValue; // range 0 to 16,777,215 Assert.AreEqual(new byte[] { 255, 255, 255 }, value.GetBytes()); Assert.AreEqual(new Bit[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, value.GetBits()); value = (UInt24)2; Assert.AreEqual(new byte[] { 2, 0, 0 }, value.GetBytes()); Assert.AreEqual(new Bit[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, value.GetBits()); // test overflow value = (UInt24)18000000; Assert.AreEqual(1222784, value); Assert.AreEqual(new byte[] { 128, 168, 18 }, value.GetBytes()); Assert.AreEqual(new Bit[] { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0 }, value.GetBits()); }
public void TestInt24() { UInt32 u32 = 0x11_22_33_44; Int32 i32 = 0x01_22_33_44F; UInt24 u32_24 = u32.ToUInt24(); UInt24 i32_24 = i32.ToUInt24(); UInt32 bu32 = u32_24.ToUInt32(); Int32 bi32 = i32_24.ToInt32(); Assert.Equal((UInt32)u32_24, bu32); Assert.Equal((Int32)i32_24, bi32); Assert.Equal((UInt32)((u32 << 8) >> 8), (UInt32)u32_24); Assert.Equal((Int32)((i32 << 8) >> 8), (Int32)i32_24); }
/// <summary> /// Creates an instance from latitude and longitude degrees using integer numbers encoded as a two's complement, fixed point number with 9 whole bits. /// See <see cref="IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues"/> for using real numbers for the degrees. /// </summary> /// <param name="latitudeDegrees"> /// A 24-bit latitude degree value encoded as a two's complement, fixed point number with 9 whole bits. /// Positive degrees correspond to the Northern Hemisphere and negative degrees correspond to the Southern Hemisphere. /// The value ranges from -90 to +90 degrees. /// </param> /// <param name="longitudeDegrees"> /// A 24-bit longitude degree value encoded as a two's complement, fixed point number with 9 whole bits. /// The value ranges from -180 to +180 degrees. /// </param> public IpV6AccessNetworkIdentifierSubOptionGeoLocation(UInt24 latitudeDegrees, UInt24 longitudeDegrees) : base(IpV6AccessNetworkIdentifierSubOptionType.GeoLocation) { LatitudeDegrees = latitudeDegrees; LongitudeDegrees = longitudeDegrees; double latitudeDegreesReal = LatitudeDegreesReal; if (latitudeDegreesReal < -90 || latitudeDegreesReal > 90) throw new ArgumentOutOfRangeException("latitudeDegrees", latitudeDegrees, string.Format(CultureInfo.InvariantCulture, "LatitudeDegreesReal is {0} and must be in [-90, 90] range.", latitudeDegreesReal)); double longitudeDegreesReal = LongitudeDegreesReal; if (longitudeDegreesReal < -180 || longitudeDegreesReal > 180) throw new ArgumentOutOfRangeException("longitudeDegrees", longitudeDegrees, string.Format(CultureInfo.InvariantCulture, "LongitudeDegreesReal is {0} and must be in [-180, 180] range.", longitudeDegreesReal)); }
protected override async ValueTask WriteFrameAsync( IFrameWriter frameWriter, CancellationToken cancellationToken = default) { var data = StreamId .SetBit(31, false); await frameWriter.WriteUInt32Async(data, cancellationToken) .ConfigureAwait(false); await frameWriter.WriteByteAsync((byte)Flags, cancellationToken) .ConfigureAwait(false); await frameWriter.WriteUInt24Async( UInt24.From((uint)Payload.Length), cancellationToken) .ConfigureAwait(false); await frameWriter.WriteBytesAsync(Payload, cancellationToken) .ConfigureAwait(false); }
public void UInt24Test() { Random random = new Random(); for (int i = 0; i != 1000; ++i) { UInt24 value = random.NextUInt24(); Assert.AreEqual(value, value); // ReSharper disable EqualExpressionComparison Assert.IsTrue(value == value); Assert.IsFalse(value != value); // ReSharper restore EqualExpressionComparison Assert.AreNotEqual(value, "string"); Assert.AreNotEqual(value, (UInt24)(((value & 0x00FFFF) + 1) | value & 0xFF0000)); Assert.AreNotEqual(value, (UInt24)((value & 0x00FFFF) | ((value & 0xFF0000) + 0x010000))); Assert.IsNotNull(value.GetHashCode()); Assert.AreEqual(((int)value).ToString(), value.ToString()); } }
/// <summary> /// Validates and parses 3-byte length sample value tag. /// </summary> /// <param name="buffer">Buffer containing sampled value.</param> /// <param name="tag">Sampled value tag to parse.</param> /// <param name="index">Start index of buffer where tag length begins - will be auto-incremented.</param> public static UInt24 ParseUInt24Tag(this byte[] buffer, SampledValueTag tag, ref int index) { if ((SampledValueTag)buffer[index] != tag) { throw new InvalidOperationException($"Encountered out-of-sequence or unknown sampled value tag: 0x{buffer[index].ToString("X").PadLeft(2, '0')}"); } index++; int tagLength = buffer.ParseTagLength(ref index); if (tagLength < 3) { throw new InvalidOperationException($"Unexpected length for \"{tag}\" tag: {tagLength}"); } UInt24 result = BigEndian.ToUInt24(buffer, index); index += tagLength; return(result); }
public void TestUInt24() { var rnd = new System.Random(); var mem = new byte[sizeof(UInt24)]; var span = new Span <byte>(mem); var roSpan = new Span <byte>(mem); for (var c = 0; c < count; c++) { span.Fill(rnd.NextByte()); UInt24 a = 0; // Pick any number except zero while ((a = rnd.NextUInt32().ToUInt24()) == 0) { } span.Write(a); // Ensure span is not zero Assert.NotEqual(0, span.ToArray().Select(b => (int)b).Sum()); var r = roSpan.ReadUInt24(); Assert.Equal(a, r); } }
public void TestVLQUInt24() { var rnd = new Random(); for (var c = 0; c < count; c++) { var memSize = rnd.Next(1, 10_000); var mem = new byte[memSize * SpanUtils.MeasureVLQ((UInt24)UInt24.MaxValue) + 1]; var span1 = new SpanStream(mem); var span2 = new SpanStream(mem); var data = new UInt24[memSize]; for (var i = 0; i < memSize; i++) { data[i] = rnd.NextUInt32().ToUInt24(); } for (var i = 0; i < memSize; i++) { span1.WriteVLQ(data[i]); } for (var i = 0; i < memSize; i++) { Assert.Equal(data[i], span2.ReadVLQUInt24(out var len)); Assert.Equal(SpanUtils.MeasureVLQ(data[i]), len); } // Check overflow new Span <byte>(mem).Fill(0xFF); Assert.Throws <OverflowException>(() => { var span3 = new SpanStream(mem); span3.ReadVLQUInt24(out _); }); } }
public void UInt24Test() { UInt24 value = (UInt24)0x010203; byte[] buffer = new byte[UInt24.SizeOf]; buffer.Write(0, value, Endianity.Big); Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Big)); Assert.AreEqual(0x01, buffer[0]); Assert.AreEqual(0x02, buffer[1]); Assert.AreEqual(0x03, buffer[2]); int offset = 0; buffer.Write(ref offset, value, Endianity.Big); Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Big)); Assert.AreEqual(3, offset); offset = 0; Assert.AreEqual(value, buffer.ReadUInt24(ref offset, Endianity.Big)); Assert.AreEqual(3, offset); buffer.Write(0, value, Endianity.Small); Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Small)); Assert.AreEqual(0x03, buffer[0]); Assert.AreEqual(0x02, buffer[1]); Assert.AreEqual(0x01, buffer[2]); offset = 0; buffer.Write(ref offset, value, Endianity.Small); Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Small)); Assert.AreEqual(3, offset); offset = 0; Assert.AreEqual(value, buffer.ReadUInt24(ref offset, Endianity.Small)); Assert.AreEqual(3, offset); }
/// <summary> /// Creates an instance from latitude and longitude degrees using integer numbers encoded as a two's complement, fixed point number with 9 whole bits. /// See <see cref="IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues"/> for using real numbers for the degrees. /// </summary> /// <param name="latitudeDegrees"> /// A 24-bit latitude degree value encoded as a two's complement, fixed point number with 9 whole bits. /// Positive degrees correspond to the Northern Hemisphere and negative degrees correspond to the Southern Hemisphere. /// The value ranges from -90 to +90 degrees. /// </param> /// <param name="longitudeDegrees"> /// A 24-bit longitude degree value encoded as a two's complement, fixed point number with 9 whole bits. /// The value ranges from -180 to +180 degrees. /// </param> public IpV6AccessNetworkIdentifierSubOptionGeoLocation(UInt24 latitudeDegrees, UInt24 longitudeDegrees) : base(IpV6AccessNetworkIdentifierSubOptionType.GeoLocation) { LatitudeDegrees = latitudeDegrees; LongitudeDegrees = longitudeDegrees; double latitudeDegreesReal = LatitudeDegreesReal; if (latitudeDegreesReal < -90 || latitudeDegreesReal > 90) { throw new ArgumentOutOfRangeException("latitudeDegrees", latitudeDegrees, string.Format(CultureInfo.InvariantCulture, "LatitudeDegreesReal is {0} and must be in [-90, 90] range.", latitudeDegreesReal)); } double longitudeDegreesReal = LongitudeDegreesReal; if (longitudeDegreesReal < -180 || longitudeDegreesReal > 180) { throw new ArgumentOutOfRangeException("longitudeDegrees", longitudeDegrees, string.Format(CultureInfo.InvariantCulture, "LongitudeDegreesReal is {0} and must be in [-180, 180] range.", longitudeDegreesReal)); } }
private static UInt24 HostToNetworkOrder(UInt24 value) { UInt24 result; unsafe { UInt24* resultPtr = &result; byte* resultBytePtr = (byte*)resultPtr; UInt24* valuePtr = &value; byte* valueBytePtr = (byte*)valuePtr; resultBytePtr[0] = valueBytePtr[2]; resultBytePtr[1] = valueBytePtr[1]; resultBytePtr[2] = valueBytePtr[0]; } return result; }
/// <summary> /// Writes the given value to the buffer using the given endianity. /// </summary> /// <param name="buffer">The buffer to write the value to.</param> /// <param name="offset">The offset in the buffer to start writing.</param> /// <param name="value">The value to write.</param> /// <param name="endianity">The endianity to use when converting the value to bytes.</param> public static void Write(this byte[] buffer, int offset, UInt24 value, Endianity endianity) { if (IsWrongEndianity(endianity)) value = HostToNetworkOrder(value); Write(buffer, offset, value); }
private static double ToReal(UInt24 twosComplementFixedPointWith9WholeBits) { return(((double)(-(twosComplementFixedPointWith9WholeBits >> 23) * (1 << 23)) + (twosComplementFixedPointWith9WholeBits & 0x7FFFFF)) / (1 << 15)); }
/// <summary> /// Converts integer value to a radix value. /// </summary> /// <param name="value">Integer value to convert.</param> /// <returns>Radix value string.</returns> public string Encode(UInt24 value) => m_uint24.Encode(value);
public void CopyBytes(UInt24 value, byte[] destinationArray, int destinationIndex) { m_copyBuffer(UInt24.GetBytes(value), 0, destinationArray, destinationIndex, 3); }
private static unsafe void Write(byte[] buffer, int offset, UInt24 value) { fixed(byte *numPtr = &buffer[offset]) * (UInt24 *)numPtr = value; }
public static void Write(this byte[] buffer, ref int offset, UInt24 value, Endianity endianity) { ByteArrayExtensions.Write(buffer, offset, value, endianity); offset += 3; }
public static UInt24 SetBits(this UInt24 source, UInt24 bits) { return (source | bits); }
public override Tag ReadTag() { var bytes = FlvReader._binaryReader.ReadBytes(4); if (bytes.Length == 0) { return null; } if (bytes.Length != 4) { throw new InvalidOperationException(string.Format("Tag consists of at least 11 bytes, but only {0} bytes were read from Stream.", bytes.Length)); } var payloadSize = new UInt24(bytes.Skip(1).Take(3), Endianness.BigEndian); var numberOfAdditionalBytesToRead = 7 + (int)payloadSize.Value; var additionalBytes = FlvReader._binaryReader.ReadBytes(numberOfAdditionalBytesToRead); if (additionalBytes.Length != numberOfAdditionalBytesToRead) { throw new InvalidOperationException(string.Format("Tag consists of {0} bytes, but only {1} bytes were read from Stream.", bytes.Length + numberOfAdditionalBytesToRead, bytes.Length + additionalBytes.Length)); } FlvReader.MoveToBeforeBackpointerState(); return new Tag(bytes.Concat(additionalBytes).ToArray()); }
public byte[] GetBytes(UInt24 value) { return m_coerceByteOrder(UInt24.GetBytes(value)); }
/// <summary>Performs proper bitwise conversion between unsigned and signed value</summary> /// <remarks> /// <para>This function is useful because CType(n, Int24) will throw an OverflowException for values greater than Int24.MaxValue.</para> /// <para>For example, this function correctly converts unsigned 24-bit integer 16777215 (i.e., UInt24.MaxValue) to signed 24-bit integer -1.</para> /// </remarks> /// <param name="unsignedInt">Unsigned UInt24 that is passed in to be converted to a signed Int24.</param> /// <returns>The Int24 value.</returns> public static Int24 ToInt24(UInt24 unsignedInt) { return Int24.GetValue(UInt24.GetBytes(unsignedInt), 0); }
public byte[] GetBytes(UInt24 value) { return(m_coerceByteOrder(UInt24.GetBytes(value))); }
protected TLS13Layer(HandshakeType msg_type, UInt24 length, ushort protocolVersion) { this.msg_type = msg_type; this.length = length; ProtocolVersion = protocolVersion; }
public static UInt24 ToggleBits(this UInt24 source, UInt24 bits) { return (source ^ bits); }
public static UInt24 EndianSwap24(this UInt24 value) => (value.Value & 0x000000FFU) << 16 | value.Value & 0x0000FF00U | (value.Value & 0x00FF0000U) >> 16;
public static bool CheckBits(this UInt24 source, UInt24 bits, bool allBits) { return (allBits ? ((source & bits) == bits) : ((source & bits) != 0)); }
public static UInt24 SetMaskedValue(this UInt24 source, UInt24 bitMask, UInt24 value) { return ((source & ~bitMask) | value); }
public static bool CheckBits(this UInt24 source, UInt24 bits) { return CheckBits(source, bits, true); }
public static Int32 ToInt32(this UInt24 value) => (Int32)value;
protected override void Given() { // 0100 1100 0100 1011 0100 0000 _uint24 = UInt24.From(5000000); }
/// <summary> /// Writes an unsigned 24-bit integer converted to a specific Endianness to the accessor. /// </summary> /// <param name="mem">The specified MemoryMappedViewAccessor.</param> /// <param name="position">The number of bytes into the accessor at which to begin writing.</param> /// <param name="value">The value to write.</param> /// <param name="convertTo">The Endianness to convert to.</param> public static void Write(this MemoryMappedViewAccessor mem, long position, UInt24 value, Endianness convertTo) { mem.Write(position, value.ConvertFromSystemEndian(convertTo)); }
private static void Write(byte[] buffer, int offset, UInt24 value) { unsafe { fixed (byte* ptr = &buffer[offset]) { *((UInt24*)ptr) = value; } } }
public static UInt24 SetMaskedValue(this UInt24 source, Bits bitMask, UInt24 value) { checked { return SetMaskedValue(source, (UInt24)bitMask, value); } }
/// <summary> /// Writes the given value to the buffer using the given endianity and increments the offset by the number of bytes written. /// </summary> /// <param name="buffer">The buffer to write the value to.</param> /// <param name="offset">The offset in the buffer to start writing.</param> /// <param name="value">The value to write.</param> /// <param name="endianity">The endianity to use when converting the value to bytes.</param> public static void Write(this byte[] buffer, ref int offset, UInt24 value, Endianity endianity) { Write(buffer, offset, value, endianity); offset += UInt24.SizeOf; }
public static UInt24 GetMaskedValue(this UInt24 source, UInt24 bitMask) { return (source & bitMask); }
/// <summary>Performs proper bitwise conversion between signed and unsigned value</summary> /// <remarks> /// <para>This function is useful because CType(n, UInt24) will throw an OverflowException for values less than zero.</para> /// <para>For example, this function correctly converts signed 24-bit integer -8388608 (i.e., Int24.MinValue) to unsigned 24-bit integer 8388608.</para> /// </remarks> /// <param name="signedInt">Signed integer that is passed in to be converted to an unsigned integer.</param> /// <returns>The unsigned integer value.</returns> public static UInt24 ToUInt24(Int24 signedInt) { return(UInt24.GetValue(Int24.GetBytes(signedInt), 0)); }
public static UInt24 ClearBits(this UInt24 source, UInt24 bits) { return (source & ~bits); }
public ValueTask WriteUInt24Async( UInt24 value, CancellationToken cancellationToken = default) => _frameWriter.WriteUInt24Async( value, LinkCancellationTokens(cancellationToken));
/// <summary>Performs proper bitwise conversion between unsigned and signed value</summary> /// <remarks> /// <para>This function is useful because CType(n, Int24) will throw an OverflowException for values greater than Int24.MaxValue.</para> /// <para>For example, this function correctly converts unsigned 24-bit integer 16777215 (i.e., UInt24.MaxValue) to signed 24-bit integer -1.</para> /// </remarks> /// <param name="unsignedInt">Unsigned UInt24 that is passed in to be converted to a signed Int24.</param> /// <returns>The Int24 value.</returns> public static Int24 ToInt24(UInt24 unsignedInt) { return(Int24.GetValue(UInt24.GetBytes(unsignedInt), 0)); }
public static UInt32 ToUInt32(this UInt24 value) => (UInt32)value;
private static double ToReal(UInt24 twosComplementFixedPointWith9WholeBits) { return ((double)(-(twosComplementFixedPointWith9WholeBits >> 23) * (1 << 23)) + (twosComplementFixedPointWith9WholeBits & 0x7FFFFF)) / (1 << 15); }