0

Is there a simple way to downcast an array of integers into an array of bytes?
Essentially, I would like to do the following thing (which does not work as is):

int[] myIntArray = new int[20];
byte[] byteArray = (byte[])myInArray;

The reason for doing this is that in my application myIntArray is actually a byte[], but was declared as an int[]. Meaning that only the least significant byte in myIntArray is of interest.

11
  • 2
    You know that the int array contains values that will fit in a byte, but the compiler doesn't. You'll have to cast each array element to a byte. Commented Aug 19, 2019 at 12:26
  • myIntArray is actually a byte[], but was declared as an int[] - How did you get that to happen? P/Invoke? If so, correct the P/Invoke signature. Commented Aug 19, 2019 at 12:28
  • I wish I could, but I have to use this crappy 3rd party dll. Interestingly, their Python equivalent does not have this problem. Yes, the array of int[] only contains values between 0 and 255, which is the output from a first method. A second method however requires this output, but as a byte[] instead of the int[], lol Commented Aug 19, 2019 at 12:29
  • Can't you correct the P/Invoke signature in your C# code? But wait, if the int array contains values like 0x00000001, 0x00000002, 0x00000003 then it is NOT an array of bytes at all; it really is an array of ints. Commented Aug 19, 2019 at 12:32
  • 1
    Assuming this 3rd party DLL is unmanaged, you must somewhere have a P/Invoke DllImport declaration. What does that look like? However, I'm thinking that what you have really is an array of int values (occupying 4 bytes each, but with only the lsb being non-zero), so you really can't cast it or fix it by changing the DLLImport declaration. Commented Aug 19, 2019 at 12:35

2 Answers 2

5

You might think this would work:

byte[] myByteArray = myIntArray.Cast<byte>().ToArray();

But it doesnt - see Why Enumerable.Cast raises an InvalidCastException?

You can use Select though to project to a new array.

byte[] myByteArray = myIntArray.Select(i => (byte)i).ToArray();

Live demo: https://rextester.com/KVR50332

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

1 Comment

Thanks, I guess I will have to do just that :-)
3

Try using Linq Select

byte[] byteArray = myIntArray.Select(i=> (byte)i).ToArray();

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.