5

Say I have an array of bytes:

byte[] input = { 0xFF, 0xFc, 0x00, 0x00 }

You can use Buffer.BlockCopy to copy bytes from one array to another, regardless of type. So, I can do this:

uint[] output = new uint[1];
Buffer.BlockCopy(input , 0, output, 0, input.Length);

This will copy the bytes from input to output, converting them from an array of bytes to an array of uints along the way.

The problem is that BlockCopy interprets the bytes with little endianness. I need a copy that uses big endianness. So rather than getting a uint value of 4294705152 (0xFFFC0000), like I need, I get the value 64767 (0x0000FCFF). Note that this is a bare-bones example, I cannot easily reverse the order of the bytes in my actual application.

Is there anything with the convenience and speed of BlockCopy that includes the ability to set the endianness I need?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
jpreed00
  • 893
  • 8
  • 25
  • It is important to note that BlockCopy endianness is system dependent, it does not necessarily interpret the bytes as little endian, this is generally on Windows based systems. (form Microsoft docs https://learn.microsoft.com/en-us/dotnet/api/system.buffer.blockcopy?view=netframework-4.8 ) : "... which byte of array element -49 is accessed first depends on the endianness of the computer that is executing your application..." . – shelbypereira Feb 28 '20 at 10:08

1 Answers1

3

The topic seems to be covered here:

How to get little endian data from big endian in c# using bitConverter.ToInt32 method?

However the conversion has to be iterated on the entire input array.

Community
  • 1
  • 1
Codor
  • 17,447
  • 9
  • 29
  • 56
  • 1
    Yeah, I was hoping there was some super-secret hacker method of doing it without having to use the BitConverter. There's a significant loss of speed and I have lots of data that would need to be handled. – jpreed00 Mar 18 '14 at 20:28