You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
841 B
31 lines
841 B
using System;
|
|
using System.Buffers.Binary;
|
|
|
|
public struct ExtendInt : IBytesConverter
|
|
{
|
|
public int Value { get; set;}
|
|
|
|
public byte[] GetBytes()
|
|
{
|
|
var result = BitConverter.GetBytes(Value);
|
|
if (BitConverter.IsLittleEndian)
|
|
{
|
|
Array.Reverse(result);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public void SetBytes(byte[] bytes, int offset = 0)
|
|
{
|
|
byte[] subBytes = new byte[GetSize()];
|
|
Array.Copy(bytes, offset, subBytes, 0, GetSize());
|
|
Value = BinaryPrimitives.ReadInt32BigEndian(subBytes);
|
|
}
|
|
|
|
public int GetSize() => sizeof(int);
|
|
public override string ToString() => Value.ToString();
|
|
|
|
static public implicit operator int(ExtendInt value) => value.Value;
|
|
static public implicit operator ExtendInt(int value) => new ExtendInt { Value = value };
|
|
}
|