473,472 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4501
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
2
by: Tomas Deman | last post by:
Hi, I need a fast method for converting an int array to a byte array. At the moment, I'm using this: public static byte Int2ByteArray(int array) { byte lbytRetval = new byte; int lintIdxHi;...
11
by: Chris Online | last post by:
Hi all, I'm using C++ Builder5. I want to get data from an edit-box and send it to a development kit. The dev-kit can only receive char and no char* here's a part of my code: char* Data_byte...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
4
by: Ciaran | last post by:
Hi there, I'm not very good at c and I was wondering if anyone could give me a hand converting the following code snippet to c# ... BYTE checksum(BYTE *InStr, BYTE len) { BYTE i, sum = 0;
10
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...
8
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,...
3
by: news.microsoft.com | last post by:
Hello, I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue. Steping through both the C# and VB.NET apps i get identical results but for one section. Notes: bit_buffer = 360...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
1
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...
0
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...

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.