1

The PacketDotNet library defines a struct UInt128. See:

How to convert this type into a simple byte array in C#?

Pythoneer
  • 87
  • 6
  • If you are simply including the source (instead of using the whole library), simply modify from `private` to `public` its readonly fields – xanatos Jan 21 '21 at 14:02
  • I'm the author of PacketDotNet. Any suggestions for improving this would be welcome, feel free to open an issue on the GitHub repo! – Chris Morgan Jan 21 '21 at 19:52

4 Answers4

2

Looks like you assign it to a BigInteger variable and use the ToByteArray() method on that.

UInt128 n128 = 12345;
BigInteger bi = (BigInteger)n128;
byte[] ba = bi.ToByteArray();
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • 1
    Note that `((BigInteger)UInt128.MaxValue).ToByteArray().Length == 17`. An extra byte is added for the sign. – xanatos Jan 21 '21 at 14:05
2

It can be cast to a BigTnteger then call ToByteArray() as below.

var number = new Uint128();

((BigInteger)number).ToByteArray();
Ben
  • 757
  • 4
  • 14
  • Note that `((BigInteger)UInt128.MaxValue).ToByteArray().Length == 17`. An extra byte is added for the sign. – xanatos Jan 21 '21 at 14:04
2

I've also discovered myself another possibility:

public static unsafe byte[] GetBytesFromUInt128(this UInt128 value)
{
    byte[] numArray = new byte[16];
    fixed (byte* numPtr = numArray)
        *(UInt128*) numPtr = value;
    return numArray;
}
Pythoneer
  • 87
  • 6
1

Some funny tricks that you can do with newer .net core:

var ui = UInt128.MaxValue - 1;

{
    var span = MemoryMarshal.Cast<UInt128, byte>(MemoryMarshal.CreateReadOnlySpan(ref ui, 1));

    for (int i = 0; i < UInt128.SizeOf; i++)
    {
        Console.WriteLine(span[i]);
    }
}

and/or given

[StructLayout(LayoutKind.Explicit)]
public struct UInt128Split
{
    [FieldOffset(0)]
    public UInt128 UInt128;

    [FieldOffset(0)]
    public ulong ULong1;

    [FieldOffset(1)]
    public ulong ULong2;

    [FieldOffset(0)]
    public uint UInt1;

    [FieldOffset(4)]
    public uint UInt2;

    [FieldOffset(8)]
    public uint UInt3;

    [FieldOffset(12)]
    public uint UInt4;

    [FieldOffset(0)]
    public byte Byte1;

    [FieldOffset(1)]
    public byte Byte2;

    [FieldOffset(2)]
    public byte Byte3;

    [FieldOffset(3)]
    public byte Byte4;

    [FieldOffset(4)]
    public byte Byte5;

    [FieldOffset(5)]
    public byte Byte6;

    [FieldOffset(6)]
    public byte Byte7;

    [FieldOffset(7)]
    public byte Byte8;

    [FieldOffset(8)]
    public byte Byte9;

    [FieldOffset(9)]
    public byte Byte10;

    [FieldOffset(10)]
    public byte Byte11;

    [FieldOffset(11)]
    public byte Byte12;

    [FieldOffset(12)]
    public byte Byte13;

    [FieldOffset(13)]
    public byte Byte14;

    [FieldOffset(14)]
    public byte Byte15;

    [FieldOffset(15)]
    public byte Byte16;
}

then

var ui = UInt128.MaxValue - 1;

{
    ref UInt128Split rf = ref Unsafe.As<UInt128, UInt128Split>(ref ui);

    Console.WriteLine(rf.UInt1);
    Console.WriteLine(rf.UInt2);
    Console.WriteLine(rf.UInt3);
    Console.WriteLine(rf.UInt4);

    Console.WriteLine(rf.ULong1);
    Console.WriteLine(rf.ULong2);

    // rf is the same as ui, so if we modify rf we modify ui!
    Console.WriteLine(ui);
    rf.ULong1 -= 11454;
    Console.WriteLine(ui);
}
xanatos
  • 109,618
  • 12
  • 197
  • 280