I have an UINT32 array filled with bytes, because my value is too long it has to be in uint32 because byte array is just to short for it
UInt32[] somebytes = new UInt32[] { 0xAABBCC, 0xAABBCC };
but whats inside is 0x00AABBCC, 0x00AABBCC
and because of that my function that should return first 2 bytes in every row
[0] = 0xAA
[1] = 0xAA
returns
[0] = 0x00
[1] = 0x00
here is my function, that should return first 2 bytes (my brain tells me that first 2 bytes are 0xAA)
public byte[] GetRed(UInt32[] buffer)
{
byte[] output = new byte[buffer.Length];
for (int i = 0; i < output.Length; i++)
{
byte b = (byte)buffer[i];
output[i] = (byte)(b & 0xFFFF); //fist 2 bytes
}
return output;
}
I hope someone has an idea, on whats going on and how to fix all this Thsnk
0x00AABBCCis0x00, not0xAA. Why not store your values in abyte[]and not worry about endian-ness and everything else that you get with multi-byte integral types. Otherwise, learn how to do bit sizzling with shifts and masks.0xAABBCCis0x00when it is stored in aUInt32variable. But also, your bitmask is wrong and your cast tobyteis cutting off the data you want. the bitmask0xFFFFwill select the opposite of what you want (0xBBCC) so you need to dob & 0xFF0000and then you need to right shift it 16 bits so that the data is in the LSBs when you cast tobyteSo your whole operation would be(byte)((b & 0xFF0000) >> 16)UInt32out and you would just be left with0! If we look at0x00AABBCC,AAis in bits 16 - 23 and you need them in bits 0 - 7 (so that they can be put into abytevariable. So that means we need to shift right 16 bits.