Leon_Amirreza said:
How Can I cast a uint type to a byte[4]?
Does the following code do this?
uint a = 5;
byte[] b = new byte[4];
b = (byte[4])a;
Hi,
No, it doesn't. Also, remember arrays are zero-based, therefor byte[4] is
invalid.
You could do it like this (little endian):
///
uint a = 5;
byte[] b = new byte[4];
b[0] = (byte)((a & 0x000000FF));
b[1] = (byte)((a & 0x0000FF00) >> 8);
b[2] = (byte)((a & 0x00FF0000) >> 16);
b[3] = (byte)((a & 0xFF000000) >> 24);
///
The extra brackets on the b[0] line are just there for making things
pretty.
You can take them out, if you want.