1

I have an array of bytes representing a picture(bgra). I'd like to know which is the order for the bytes in a c# byte[]. Are the bytes represented in little or big endian ?

Thank you for reading.

user2417992
  • 204
  • 1
  • 6
  • 16

2 Answers2

4

Luke's is right but ii wonder if one could write code to prove it (wihout BitConvert.IsLittleEndian )

ushort word = 0x0001;  // 16bit word with lsb set
var bits = new BitArray(BitConvert.GetBytes());

if (bits[0]) {
    // little endian
} else if (bits[8]) { 
    // big endian
}

edit for Sinatr

Quinton Bernhardt
  • 4,773
  • 19
  • 28
  • I like it, its a really clean proof – undefined Jun 07 '13 at 12:20
  • I thought endian is something to do with `bytes`, not bits. Can you please elaborate? I would try to store a word (which has only low byte set) somehow and then read it as a byte array and see if the first byte is zero or not. – Sinatr Jun 07 '13 at 12:49
2

Windows is little endian, so if you are running c# you are probably using little endian

undefined
  • 33,537
  • 22
  • 129
  • 198
  • Okay thanks, but then is it possible to swap the order to big Endian(it may be a very stupid questino but i have a problem with picture represention and i think it come from the order in a byte array). – user2417992 Jun 07 '13 at 12:14
  • This answer doesn't help. It doesn't depend on C#, but on the origin of the byte array. You could use both (little or big endian) in C#. – ken2k Jun 07 '13 at 12:16
  • 1
    Check out here for how to reverse endianness in c# http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/144b3047-bcbd-48d6-a28a-6fbda82e151e – undefined Jun 07 '13 at 12:18