471,319 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Converting byte[] into byte?[]

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.

Nov 29 '05 #1
6 3920
Narshe,

There is an easier way. In .NET 2.0 (which you must be using because
you are using the nullable type), you can do:

nullBytes = Array.ConvertAll<byte, byte?>(bytes,
delegate(byte b)
{
return b;
});

This works because there is an implicit cast between byte and byte?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Narshe" <na****@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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.

Nov 29 '05 #2
There must be an easier/better way.


No that's the way to do it.
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 29 '05 #3
Narshe wrote:
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.


I suspect it's not something which is needed particularly often, so I
doubt that there *is* a better way. You could probably write a method
which does it in a generic way, so you'd only have to call that method
whatever type you had.

Even if the framework provided some way itself, it would just be doing
the for loop internally...

Jon

Nov 29 '05 #4
>
Even if the framework provided some way itself, it would just be doing
the for loop internally...

Jon

Of course, but it is preferable to use 'system' methods,
whenever possible, than to build your own.
Roger
Nov 29 '05 #5
but also beware of the implication.

in this case, using ConvertAll, for each iteration, you are making a
delegate call which is significantly slower than a direct call, and because
it can't be inlined, an extra stackframe as well, for something as simple as
convert from byte to byte? This overhead may or may not be an issue.

generally speaking though, I too prefer using the provided generics/delegate
based collection methods. it hides the loop and leaves only the logic and
results in cleaner code imo.

"Roger" wrote:

Even if the framework provided some way itself, it would just be doing
the for loop internally...

Jon

Of course, but it is preferable to use 'system' methods,
whenever possible, than to build your own.
Roger

Nov 29 '05 #6
Daniel,

You know they sped up delegate invocation for .NET 2.0, right?

I would say if this is an issue for you, you are going to hate LINQ =)
(I know you won't, you stated your preference in your post, but I couldn't
help but think this when you responded).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com...
but also beware of the implication.

in this case, using ConvertAll, for each iteration, you are making a
delegate call which is significantly slower than a direct call, and
because
it can't be inlined, an extra stackframe as well, for something as simple
as
convert from byte to byte? This overhead may or may not be an issue.

generally speaking though, I too prefer using the provided
generics/delegate
based collection methods. it hides the loop and leaves only the logic and
results in cleaner code imo.

"Roger" wrote:
>
> Even if the framework provided some way itself, it would just be doing
> the for loop internally...
>
> Jon
>

Of course, but it is preferable to use 'system' methods,
whenever possible, than to build your own.
Roger

Nov 29 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by matt melton | last post: by
4 posts views Thread by Hal Vaughan | last post: by
2 posts views Thread by Tomas Deman | last post: by
11 posts views Thread by Chris Online | last post: by
4 posts views Thread by Ciaran | last post: by
10 posts views Thread by JT | last post: by
3 posts views Thread by news.microsoft.com | last post: by
reply views Thread by rosydwin | 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.