471,594 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

short[] to byte[] conversion

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[] );

--
Alberto Cardoso

Nov 16 '06 #1
9 15647
I dont think there is. You will need to loop through them.

Ciaran O'Donnell

"Alberto Cardoso" wrote:
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[] );

--
Alberto Cardoso
Nov 16 '06 #2
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '06 #3
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;});

--
HTH
Stoitcho Goutsev (100)

"Alberto Cardoso" <Al************@discussions.microsoft.comwrote in
message news:F9**********************************@microsof t.com...
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[] );

--
Alberto Cardoso

Nov 17 '06 #4
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.
Nov 17 '06 #5
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.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Nov 17 '06 #6
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '06 #7
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?

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:eM**************@TK2MSFTNGP06.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 18 '06 #8
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 18 '06 #9
Hi Mattias,
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?
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!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Nov 18 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Selvin | last post: by
5 posts views Thread by Igor Natachanny | last post: by
4 posts views Thread by Richard506 | last post: by
1 post views Thread by Tim Wagaman | last post: by
4 posts views Thread by Lew Barnesson | last post: by
2 posts views Thread by =?Utf-8?B?QmFydE1hbg==?= | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.