I want to add two 32-bit values into byte[8] in big endian style.
int a = 1; // => [ 0, 0, 0, 1]
int b = 2; // => [ 0, 0, 0, 2]
resulting in
[0, 0, 0, 1, 0, 0, 0, 2]
So what is the one liner for this?
My approach so far was
int a = 1;
int b = 2;
byte[] array = new byte[] { };
array = array.Concat(BitConverter.GetBytes(a)).ToArray();
array = array.Concat(BitConverter.GetBytes(b)).ToArray();
so this is already complicated enough but little endian. So I would add a line for each int to get them reversed.
Two talk in python:
a = 1;
b = 2;
array = a.to_bytes(4, 'big') + b.to_bytes(4, 'big')
What is the C# method for this?
var array = BitConverter.GetBytes(a).Concat(BitConverter.GetBytes(b)).ToArray()instead of e.g. creating an empty array first and storing an intermediate form where justahas been added to the array. You notably don't seem to do these same things in the Python version...BinaryPrimitiveslibrary if you have access to dotNET 3.1 in your project.