473,729 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ 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 4570
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.ConvertAl l<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.co m

"Narshe" <na****@gmail.c om> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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.co m

"Daniel Jin" <Da*******@disc ussions.microso ft.com> wrote in message
news:09******** *************** ***********@mic rosoft.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
13923
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
4
10307
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 bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation I'm not aware of that could cause problems. I'm doing this because it's easier to do some of my...
2
2678
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 there any way. Regards, Govind.
2
407
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; int lintIdxLo; for (int i = 0; i < array.GetLength(0); i++)
11
3222
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 = 0x00; UCHAR outBuffer;
8
4564
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 XML, and embedded the array as some child element in an xml file, i.e.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
4
1688
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
11388
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 (byte tmpByte in bytes) hexString.Append(tmpByte.ToString("X2")); int intValue = int.Parse(hexString.ToString(), NumberStyles.AllowHexSpecific);
8
4198
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, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
3
1538
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 and bArray is an array of bytes going into these lines of code **********
0
8924
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8155
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6026
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2689
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.