-1

I have a group of integers that I want to send over a socket connection. What would be the cleanest way to convert the following variables into a byte array?

int var1 = 1;
int var2 = 2;
int var3 = 3;

Because I'm sending data via sockets, I would like to format the data using big-endianness. Thus for the three variables listed, I would want a 12 byte array such that

byte[0] = 0;
byte[1] = 0;
byte[2] = 0;
byte[3] = 1;
byte[4] = 0;
byte[5] = 0;
byte[6] = 0;
byte[7] = 2;
byte[8] = 0;
byte[9] = 0;
byte[10] = 0;
byte[11] = 3;

My search results are surprisingly coming up empty.

Edit: The suggested duplicate questions are different from what I'm asking. I do not have an "array of integers" or "a single integer" that I want to convert to a byte array. I have a group of individual array variables and am looking for the cleanest solution.

Izzo
  • 4,461
  • 13
  • 45
  • 82
  • `Buffer.BlockCopy`... – Johnny Aug 05 '19 at 19:41
  • *"My search results are surprisingly coming up empty* I don't see how that's possible...I searched for *"convert integers to byte array c# site:stackoverflow.com"* and it returned most of the duplicates for this question. Unless there's something else you were asking that I misunderstood? – Rufus L Aug 05 '19 at 19:46
  • @RufusL I edited my question to clarify how my question is unique. – Izzo Aug 05 '19 at 19:52
  • Can you show some sample input and expected output to show what result you're looking for? How to you want to combine the integers so you end up with a single byte array (aside from adding them to an `int[]`, which it sounds like you don't want to do)? Or do you want a `byte[byte[]]` result? – Rufus L Aug 05 '19 at 20:34
  • `byte[] myMessage = new byte[3 * sizeof(int)]; Buffer.BlockCopy(new[] {var1, var2, var3}, 0, myMessage, 0, myMessage.Length);` – Rufus L Aug 05 '19 at 20:40
  • @RufusL I edited my question with more information. – Izzo Aug 05 '19 at 21:25

1 Answers1

1

One way to do this would be to write a method that takes in a params int[] arg, which allows you to pass any number of int values to it.

Then you can create a byte[] based on the number of arguments, and populate it in a loop.

Since you want a Big-Endian result, we can use Array.Reverse to reverse the order of bytes returned from BitConverter.GetBytes for each argument, and then we can add each byte to the result array based on the index of the byte and the index of the argument:

public static byte[] GetBigEndianBytes(params int[] args)
{
    var result = new byte[args.Length * sizeof(int)];           

    for (var argIndex = 0; argIndex < args.Length; argIndex++)
    {
        var bytes = BitConverter.GetBytes(args[argIndex]).Reverse().ToArray();

        for (var byteIndex = 0; byteIndex < bytes.Length; byteIndex++)
        {
            result[byteIndex + argIndex * sizeof(int)] = bytes[byteIndex];
        }
    }

    return result;
}

In use, this might look like:

private static void Main()
{
    int var1 = 1;
    int var2 = 2;
    int var3 = 3;

    var result = GetBigEndianBytes(var1, var2, var3);

    // result = { 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
Rufus L
  • 36,127
  • 5
  • 30
  • 43