-1

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?

RadioMan85
  • 337
  • 1
  • 11
  • 3
    Well, some of your own choices here are a little odd. I.e. it's unclear why you don't just have `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 just `a` has been added to the array. You notably don't seem to do these same things in the Python version... – Damien_The_Unbeliever Mar 16 '21 at 13:16
  • (a<< 16) + b. You are adding two int16 values. – jdweng Mar 16 '21 at 13:18
  • 1
    There is a new [`BinaryPrimitives`](https://learn.microsoft.com/en-us/dotnet/api/system.buffers.binary.binaryprimitives?view=netcore-3.1) library if you have access to dotNET 3.1 in your project. – D M Mar 16 '21 at 13:18
  • 1
    Does this answer your question? [C# int to byte\[\]](https://stackoverflow.com/questions/1318933/c-sharp-int-to-byte) or [this](https://stackoverflow.com/questions/5896680/converting-an-int-to-byte-in-c-sharp) answer. – Trevor Mar 16 '21 at 13:18
  • @Damien_The_Unbeliever: I want that '1' and '2' gets [0,0,0,1,0,0,0,2]. This is basically converting the 32-bit '1' and '2' into a combined bytearray in big endian style. Little endian would result in [1,0,0,0,2,0,0,0] which my above C# solution unfortunately ends up with. – RadioMan85 Mar 16 '21 at 13:27
  • @jdweng, (a<< 32) + b would then be what I want... hmm, interesting. will keep that in mind... thx – RadioMan85 Mar 16 '21 at 13:29
  • Then you end up with a 64 bit long . – jdweng Mar 16 '21 at 13:35
  • @Damien_The_Unbeliever: Running on dotNET 4.7.2 framework, but "using System.Buffers.Binary" doesn't recognize "Buffer". So probably not working though. – RadioMan85 Mar 16 '21 at 13:36
  • @jdweng Jep, thats the goal – RadioMan85 Mar 16 '21 at 13:37
  • 1
    Be careful of negative numbers which is using 2'scompliment sign extension. Are your number sign or unsign? – jdweng Mar 16 '21 at 13:41
  • @jdweng Good point. both signed (Int32). – RadioMan85 Mar 16 '21 at 13:43
  • if lower order number is negative you have to sign extend before adding to upper number. Sign extension can simply be OR'ing 0xFFFF0000. – jdweng Mar 16 '21 at 13:50
  • @Codexer: Why did you remove the focus of the question?? It doesn't make much sense to shape a question just to make it fit better to another one just to earn some points for that and make this looks like a duplicate. It is not: this question is about if there is no easier way because obviously other languages support quite nice and tiny solutions for that problem. Therefore this question of "an easier" solution (not "a solution"!!) isn't set up yet and is different from these to questions you have posted above. – RadioMan85 Mar 17 '21 at 07:29
  • @Codexer: So I request you to remove the duplicate sign again because this focus on the question "can it be done better?" rather than "how does it work?". – RadioMan85 Mar 17 '21 at 07:31

1 Answers1

1

BitConverter endianness is dependent on the platform on which it is executing, so you'll have to make a check to BitConverter.IsLittleEndian and do some conditional .Reverse()-ing of the results from BitConverter.GetBytes(a).

byte[] array = (BitConverter.IsLittleEndian ?
  BitConverter.GetBytes(a).Reverse().Concat(BitConverter.GetBytes(b).Reverse()) : 
  BitConverter.GetBytes(a).Concat(BitConverter.GetBytes(b))
).ToArray();

(I wouldn't necessarily do it as a one-liner as you asked, but you can create useful functions with more economical syntax from this example.)

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • Thx for you input. Well no, I wouldn't force this into a one liner just to achieve a one liner. With one liner I for sure simply mean a short tiny pretty solution (like in python). Your solution then would violate our "max. 150 symbols on one line" policy :-) – RadioMan85 Mar 17 '21 at 07:43