Pythoneer
Pythoneer

Reputation: 107

How to extract bytes from the UInt128 type

The PacketDotNet library defines a struct UInt128. See:

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

Upvotes: 2

Views: 1277

Answers (5)

Scott Jibben
Scott Jibben

Reputation: 2287

Using System.UInt128 (.NET 7). May not be relevant to PacketDotNet.UInt128.

This is similar to other solutions but includes a flag for unsigned values. This shouldn't add the extra 17th byte that @xanatos is referring to:

UInt128 number = UInt128.Parse("338288524927261089654164245681446711638");
byte[] myBytes = ((BigInteger)number).ToByteArray(isUnsigned: true);

Upvotes: 1

xanatos
xanatos

Reputation: 111850

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);
}

Upvotes: 2

Pythoneer
Pythoneer

Reputation: 107

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;
}

Upvotes: 3

Ben
Ben

Reputation: 787

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

var number = new Uint128();

((BigInteger)number).ToByteArray();

Upvotes: 3

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

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();

Upvotes: 2

Related Questions