1

This question only vise versa. For now I got this:

UInt32[] target;

byte[] decoded = new byte[target.Length * 2];
Buffer.BlockCopy(target, 0, decoded, 0, target.Length);

And this doesn't work, I get array filled with 0x00.

Community
  • 1
  • 1
Romz
  • 1,437
  • 7
  • 36
  • 63
  • It would be wrong even if it had worked. `decoded` is only half the size it should be (an `uint` is 4 bytes, so the factor must be 4). And the last parameter to `BlockCopy` is a `byte` count, not an `uint`-count. – harold Oct 08 '13 at 19:06
  • One way can be by defining a structure using Explicit Layout. Following post may be helpful: http://social.msdn.microsoft.com/Forums/en-US/60150e7b-665a-49a2-8e2e-2097986142f3/c-equivalent-to-c-union?forum=csharplanguage – Sunny Agrawal Oct 08 '13 at 19:07
  • Which endianness? `BlockCopy` uses native endianness, which is rarely the desired behavior in my experience. – CodesInChaos Oct 08 '13 at 19:10
  • There are two weird points here: 1) Why `* 2` not `* 4`? `UInt32` is a 4 byte integer. 2) The length parameter is in bytes. So you need to use `decoded.Length` – CodesInChaos Oct 08 '13 at 19:16
  • @SunnyAgrawal: That's overkill because he doesn't have a structure. Just a `UInt32`. – poy Oct 08 '13 at 19:24

5 Answers5

3

You can use BitConverter.GetBytes method for converting a unit to byte

Anirudha
  • 32,393
  • 7
  • 68
  • 89
FredrikR
  • 69
  • 3
  • The only to `BitConverter.GetBytes()` is that if you have a large array of `UInt32s`, and you need to convert the entire thing into an array of `Bytes`... then you end up allocating quite a bit of `byte[4]s` and have to add them to your resulting array... It can actually end up being a bit slow. – poy Oct 08 '13 at 19:22
3

I would recommend something like the following:

UInt32[] target;

//Assignments

byte[] decoded = new byte[target.Length * sizeof(uint)];
Buffer.BlockCopy(target, 0, decoded, 0, decoded.Length);

See code:

uint[] target = new uint[] { 1, 2, 3 };

//Assignments

byte[] decoded = new byte[target.Length * sizeof(uint)];
Buffer.BlockCopy(target, 0, decoded, 0, decoded.Length);

for (int i = 0; i < decoded.Length; i++)
{
    Console.WriteLine(decoded[i]);
}

Console.ReadKey();

Also see:

Community
  • 1
  • 1
jrbeverly
  • 1,611
  • 14
  • 20
2

Try this code. It works for me.

UInt32[] target = new UInt32[]{1,2,3}; 
  byte[] decoded = new byte[target.Length * sizeof(UInt32)];
  Buffer.BlockCopy(target, 0, decoded, 0, target.Length*sizeof(UInt32));

    foreach(byte b in decoded)     
    {
        Console.WriteLine( b);
    }
Alezis
  • 2,659
  • 3
  • 27
  • 34
1

You need to multiple by 4 to create your byte array, since UInt32 is 4 bytes (32 bit). But use BitConverter and fill a list of byte and late you can create an array out of it if you need.

UInt32[] target = new UInt32[] { 1, 2, 3 };
byte[] decoded = new byte[target.Length * 4]; //not required now
List<byte> listOfBytes = new List<byte>();
foreach (var item in target)
{
    listOfBytes.AddRange(BitConverter.GetBytes(item));   
}

If you need array then:

byte[] decoded = listOfBytes.ToArray();
user2711965
  • 1,795
  • 2
  • 14
  • 34
1

Your code has a few errors:

UInt32[] target = new uint[] { 1, 2, 3, 4 };

// Error 1:
// You had 2 instead of 4.  Each UInt32 is actually 4 bytes.
byte[] decoded = new byte[target.Length * 4];

// Error 2:
Buffer.BlockCopy(
  src: target, 
  srcOffset: 0, 
  dst: decoded,
  dstOffset: 0, 
  count: decoded.Length // You had target.Length. You want the length in bytes.
);

This should yield what you're expecting.

poy
  • 10,063
  • 9
  • 49
  • 74