How Can I Cast DotNet Variables into a byte array?

  • Thread starter Thread starter Leon_Amirreza
  • Start date Start date
L

Leon_Amirreza

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;
 
You can't cast a type into a byte array. If you want to get an unsigned
integer into a representation of four bytes, you can call the static
GetBytes method on the BitConverter class, passing the value you want to
convert to bytes, like so:

uint a = 5;
byte[] b = BitConverter.GetBytes(a);
 
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.
 
The cast to byte[4] is not invalid because arrays are zero-based. You
can't place a length specifier in a cast, that's why it's invalid (in
addition to the fact that there is no cast from uint to byte[]).

Also, while that code should work, it's definitely much more work than
calling GetBytes on the BitConverter class. If you needed to reverse the
endianness (is that a word?) you could always pass the byte array to the
static Reverse method on the Array class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tom Spink said:
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.
 
On Jul 9, 4:39 pm, "Nicholas Paldino [.NET/C# MVP]"

Also, while that code should work, it's definitely much more work than
calling GetBytes on the BitConverter class. If you needed to reverse the
endianness (is that a word?) you could always pass the byte array to the
static Reverse method on the Array class.

Or use my EndianBitConverter class which lets you specify the
endianness :)

http://pobox.com/~skeet/csharp/miscutil

Jon
 
Nicholas said:
The cast to byte[4] is not invalid because arrays are zero-based. You
can't place a length specifier in a cast, that's why it's invalid (in
addition to the fact that there is no cast from uint to byte[]).

Also, while that code should work, it's definitely much more work than
calling GetBytes on the BitConverter class. If you needed to reverse the
endianness (is that a word?) you could always pass the byte array to the
static Reverse method on the Array class.

Hi Nicholas,

Whoops.. I read the code wrong there! I mis-read byte[4] for b[4], and
further mis-read it for an assignment. That'll teach me.

Also, the code will work, but sometimes is useful to understand how things
work. A one-liner is probably best in these cases, but knowing what that
one-liner *does* is nice.

And, of course, with my code to change the endianness, you just change the
numbers in the array indicies, rather than pass it to a method to reverse
the array.
 
Hi,

Tom Spink said:
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.

why byte[4] is invalid?
 
Ignacio Machin \( .NET/ C# MVP \) said:
Hi,

Tom Spink said:
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.

why byte[4] is invalid?

Well, I made a mistake, and took it for an assignment to b[4]. And b was
declared as an array of length 4.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top