I dont think there is. You will need to loop through them.
Ciaran O'Donnell
> 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.
[quoted text clipped - 3 lines]
>
> Bitconverter.GetBytes( short[] );
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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jonathan Wood - 17 Nov 2006 23:24 GMT
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.

Signature
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Mattias Sjögren - 18 Nov 2006 00:16 GMT
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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jonathan Wood - 18 Nov 2006 00:23 GMT
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?

Signature
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
> Hi Jonathan,
>
[quoted text clipped - 4 lines]
>
> Mattias
Mattias Sjögren - 18 Nov 2006 01:05 GMT
>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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jonathan Wood - 18 Nov 2006 19:53 GMT
Hi Mattias,
> >One other question, if I may: what are the ramifications of using
> >routines
[quoted text clipped - 17 lines]
>
> 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!

Signature
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
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;});

Signature
HTH
Stoitcho Goutsev (100)
> 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.
[quoted text clipped - 3 lines]
>
> Bitconverter.GetBytes( short[] );
Dustin Campbell - 17 Nov 2006 16:05 GMT
> There is no direct method call AFAIK, but you can use the
> Array.ConvertAll method for that
[quoted text clipped - 3 lines]
> 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.