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.

5
  • 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. Commented Oct 8, 2013 at 19:06
  • One way can be by defining a structure using Explicit Layout. Following post may be helpful: social.msdn.microsoft.com/Forums/en-US/… Commented Oct 8, 2013 at 19:07
  • Which endianness? BlockCopy uses native endianness, which is rarely the desired behavior in my experience. Commented Oct 8, 2013 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 Commented Oct 8, 2013 at 19:16
  • @SunnyAgrawal: That's overkill because he doesn't have a structure. Just a UInt32. Commented Oct 8, 2013 at 19:24

5 Answers 5

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:

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

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.
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);
    }

Comments

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();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.