Converting byte[] to Int16[]

L

Lint Radley

Hello,


I am looking for a way to cast a byte[] to Int16[].

I get this error:
"Unable to cast object of type 'System.Byte[]' to type 'System.Int16[]'"

If you're curious, I am using a library for accessing some specialized
image types. It seems I am returned an array of object's (declared as
object[], usually casted to Int16). However, some images seem to have
byte data not int16.

Any advice would be appreciated. I have been looking around the net and
it seems byte can be implicitly and explicitly cast to Int16. Why is
this not true for an array?

Thanks for all of your advice in advice,

Lint Radley
 
M

Michael C

Lint Radley said:
Hello,


I am looking for a way to cast a byte[] to Int16[].

I get this error:
"Unable to cast object of type 'System.Byte[]' to type 'System.Int16[]'"

If you're curious, I am using a library for accessing some specialized
image types. It seems I am returned an array of object's (declared as
object[], usually casted to Int16). However, some images seem to have byte
data not int16.

There's a few ways of copying arrays:
BitConverterClass
Marshal.Copy
Array.CopyTo
CopyMemory api call.

Array.CopyTo would be the best option.
Any advice would be appreciated. I have been looking around the net and it
seems byte can be implicitly and explicitly cast to Int16. Why is this not
true for an array?

The copy is really a for loop copying 1 element at a time. It is possible
that it could be done for you but it isn't.

Michael
 
J

Jon Skeet [C# MVP]

Lint Radley said:
Any advice would be appreciated. I have been looking around the net and
it seems byte can be implicitly and explicitly cast to Int16. Why is
this not true for an array?

Value type arrays are not covariant.

Would you expect each byte to be represented by one Int16, or each two
bytes? What endianness would you expect it to take?
 
?

=?gb2312?B?u9u+/A==?=

If you want convert from array S1[] to array S2[], S1 and S2 must be
reference type. The array covariance means that for any reference type
A and B, if a implicit or explicit reference conversion exist that
convert from A to B, then a same reference conversion exists from
array A[R] to B[R].
 
B

Ben Voigt

»Û¾ü said:
If you want convert from array S1[] to array S2[], S1 and S2 must be
reference type. The array covariance means that for any reference type
A and B, if a implicit or explicit reference conversion exist that
convert from A to B, then a same reference conversion exists from
array A[R] to B[R].


And this is broken!

You can have covariance for readers, contravariance for writers (and
delegates do this right, covariant input parameters, contravariant return
values and output parameters). But an array allows both reading and writing
so it has to be invariant. But Java didn't care about type safety, so C#
followed suit.
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
And this is broken!

You can have covariance for readers, contravariance for writers (and
delegates do this right, covariant input parameters, contravariant return
values and output parameters). But an array allows both reading and writing
so it has to be invariant. But Java didn't care about type safety, so C#
followed suit.

Well, it's about striking a balance. You still get type safety at
runtime, of course - "just" not at compile-time.

I'm not trying to trivialise it, but you'd have to admit that (at least
pre-generics) it did make things a lot simpler in many situations. Even
with generics, people often wish they *were* covariant/contravariant in
appropriate situations. There's an ease of use vs safety balance to be
struck.

I wouldn't like to say whether or not they made the right decision, but
even if I disagree with the decision, I don't think it can be called
"broken" - it's just the balance was struck at a place other than where
you'd want it.
 

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

Top