473,659 Members | 3,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8973
"Lint Radley" <sc********@for wardbounding.co mwrote in message
news:1oINh.2775 $yo3.160@trnddc 04...
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:
BitConverterCla ss
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********@for wardbounding.co mwrote:
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.co m>
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********@gma il.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.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.nos pamwrote:
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.co m>
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
5382
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 "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
1
1640
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 is displayed correctly......if not how will i do it byte by byte....... *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
8
3668
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 from that string, the continment is altered from it's original state. Below is a code snipped I'm using: // Here I read the wave file and I convert the result to a string byte b = new byte; FileStream fs = File.OpenRead("test.wav"); int size =...
10
11368
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 (byte tmpByte in bytes) hexString.Append(tmpByte.ToString("X2")); int intValue = int.Parse(hexString.ToString(), NumberStyles.AllowHexSpecific);
1
9558
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 first one.
8
4193
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, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
6
4553
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 from a byte to the byte?. There must be an easier/better way.
5
9605
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 class to produce values in the system. For instance I could create class MsgGenericValue<UInt16>() which would represent an unsigned value on the interface.
5
16137
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); for (i=0,i2=7; i<5; i++,i2--)
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.