473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Converting byte[] to Int16[]

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
Mar 26 '07 #1
5 8942
"Lint Radley" <sc********@forwardbounding.comwrote in message
news:1oINh.2775$yo3.160@trnddc04...
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
Mar 26 '07 #2
Lint Radley <sc********@forwardbounding.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 26 '07 #3
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].

Mar 26 '07 #4

"»Û¾ü" <Ju********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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.
Mar 30 '07 #5
Ben Voigt <rb*@nospam.nospamwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
1
by: James Dean | last post by:
i want to decode 2 byte and 1 byte char data......because japanese characters take up 2 bytes ordinary characters 1 byte.....can u convert one byte array so that the 2 byte and 1 byte information...
8
by: Marius Cabas | last post by:
Hi, I'm a beginner so don't shoot ;) I'm reading a wave file into a byte and I'm trying to convert the result to String but the converted string is altered, so if I'm generating a new wave file...
10
by: JT | last post by:
Need some help in converting a byte to a signed int. This is what I have attempted to do: byte bytes = new byte { 0xFF, 0xFF, 0x9C}; StringBuilder hexString = new StringBuilder(); foreach...
1
by: Benoit | last post by:
Hi, what is the most performing way to convert a byte stream to a string? Byte() to be converted to String. Thanks, iBen. Sorry if it ia a double post in this newsgroup, I cannot set my...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
6
by: Narshe | last post by:
How can I convert a byte into a byte?? You can assign a byte to a byte?, but you obviously can't do that with an array. The way I'm currently doing it is a for loop that just copies the data...
5
by: vtjumper | last post by:
I'm building a C# interface to an existing messaging system. The messaging system allows values of several types to be sent/recieved over the interface. What I want to do is use a generic...
5
jeffbroodwar
by: jeffbroodwar | last post by:
Hi everyone, I have a program that converts variables long,string,double to byte array here's the code : for long : //CompanyId temp = longToByteArray(CompanyId); ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.