short[] to byte[] conversion

G

Guest

Is there a direct way to convert a short array to a byte array?
I dont to use a for and cast every short to a byte.

I want something like the BitConverter class that accpets a short array as
argument.

Bitconverter.GetBytes( short[] );
 
M

Mattias Sjögren

Alberto,
Is there a direct way to convert a short array to a byte array?

Depends on what kind of behaviour you want.

If you want to make a straight memory copy so that each short results
in two bytes, you can use the System.Buffer.BlockCopy method.

If you want just one byte for each short (assuming the value is in
range), you basically have to use a loop. Either write an explcit loop
or, if you're on .NET 2.0, use a method such as
Array.ConvertAll<short, byte>().


Mattias
 
S

Stoitcho Goutsev \(100\)

Alberto,

There is no direct method call AFAIK, but you can use the Array.ConvertAll
method for that


short[] arrShort = new short[] { 100, 200, 30 };
byte[] arrByte;

arrByte = Array.ConvertAll<short, byte>(arrShort, delegate(short
item){return (byte)item;});
 
D

Dustin Campbell

There is no direct method call AFAIK, but you can use the
Array.ConvertAll method for that

short[] arrShort = new short[] { 100, 200, 30 };
byte[] arrByte;
arrByte = Array.ConvertAll<short, byte>(arrShort, delegate(short
item){return (byte)item;});

Just for the curious, here's what this looks like with lambda expressions
in C# 3.0:

short[] arrShort = new short[] { 100, 200, 30 };
byte[] arrByte = Array.ConvertAll(arrShort, item => (byte)item);

Best Regards,
Dustin Campbell
Developer Express Inc.
 
J

Jonathan Wood

Mattias,
If you want to make a straight memory copy so that each short results
in two bytes, you can use the System.Buffer.BlockCopy method.

Dang, I'm glad I ran across this post. That's exactly what I was wondering
about too.

One thing: Is there any support for copying bytes from an array to a
variable and the reverse? I realize I could do this by first creating an
array of the correct type and then assigning an element to a variable. But
is there by chance any way to do that directly?

Thanks.
 
M

Mattias Sjögren

Hi Jonathan,
One thing: Is there any support for copying bytes from an array to a
variable and the reverse?

Check out the BitConverter class.



Mattias
 
J

Jonathan Wood

Hi Mattias,

That looks like it should do the trick, thanks!

One other question, if I may: what are the ramifications of using routines
that aren't CLS-compliant? I may want to run this code on a Web server. Is
that going to be a problem?
 
M

Mattias Sjögren

One other question, if I may: what are the ramifications of using routines
that aren't CLS-compliant? I may want to run this code on a Web server. Is
that going to be a problem?

CLS is all about language interoperability and defining a lowest
common set of features that all managed languages should be able to
support. It's not related to security. So CLS is really only relevant
for people writing components that may be consumed by other languages.
If you do that, you may want to restrict your public interfaces to CLS
compliant features (or at least provide CLS compiliant alternatives).

Consuming non-CLS compilant stuff in your own code is generally not a
problem. That's an implementation detail that others don't care about.

However, one of the things that aren't CLS compilant are pointer
types, and to enable use of pointer types in C# you have to turn on
unsafe code. And _that_ may affect your code's ability to execute in
partially trusted environments.

Does that answer your question?


Mattias
 
J

Jonathan Wood

Hi Mattias,
CLS is all about language interoperability and defining a lowest
common set of features that all managed languages should be able to
support. It's not related to security. So CLS is really only relevant
for people writing components that may be consumed by other languages.
If you do that, you may want to restrict your public interfaces to CLS
compliant features (or at least provide CLS compiliant alternatives).

Consuming non-CLS compilant stuff in your own code is generally not a
problem. That's an implementation detail that others don't care about.

However, one of the things that aren't CLS compilant are pointer
types, and to enable use of pointer types in C# you have to turn on
unsafe code. And _that_ may affect your code's ability to execute in
partially trusted environments.

Does that answer your question?

I think so. Basically, it's only an issue (potentially) if I'm writing
components to be used by other .NET languages. And it should have no effect
on being used on a Web server. I think I would avoid the pointer types then
for Web programming.

Thanks!
 

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