1

I need to convert a large array of ints into an array of their little endian byte representations. Is there a function that converts the array or is the only way to loop through and "manually" convert each one.

crazywill32
  • 390
  • 1
  • 4
  • 12

1 Answers1

2

One thing you could do is use the EndianBitConverter project from Nuget. It does what it says on the tin and provides a way of converting to either big-endian or little-endian.

Once you have got it installed, you can write code like this:

var intList = new List<int> { 91233, 67278, 22345, 45454, 23449 };
            
foreach ( var n in intList)
{
    var result1 = EndianBitConverter.LittleEndian.GetBytes(n);
    var result2 = EndianBitConverter.BigEndian.GetBytes(n);
}

If you want to do it as a 1 liner, then perhaps something like this:

var intList = new List<int> { 91233, 67278, 22345, 45454, 23449 };
var result = intList.Select(x => EndianBitConverter.LittleEndian.GetBytes(x)).ToList();
Ocean Airdrop
  • 2,793
  • 1
  • 26
  • 31