473,385 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Select a section of a byte array

Airslash
221 100+
Hello,

I'm currently working with byte arrays to hold data transmitted over the network, and i'm looking for a way to copy a section of that byte array and return it from a function result.

I know I can create a temporary array and call the CopyTo function to copy the data in that array, but I was wondering this this piece of code below with LinQ would do the same.

Expand|Select|Wrap|Line Numbers
  1. return (from m in Enumerable.Range(0, Size) select RawContent[64 + m]);
  2.  
- RawContent is a property that returns a byte[] array
- Size is a long that gives the range of the data to copy
- 64 is the offset in Rawcontent to copy from.

Would this call return a byte array with the values from RawContent[64]-> RawContent[Size] ?


EDIT: changed the range setting.
Jun 24 '10 #1
2 4605
Christian Binder
218 Expert 100+
Not, this won't return an array, this will return an IEnumerable<byte>. You have to call .ToArray() to get back a byte[].

Secondly this won't return index 64 to Size but 64 to 64+Size.


You can define an offset in Enumerable.Range like Enumerable.Range(64, Size), so you don't have to write m+64.

Expand|Select|Wrap|Line Numbers
  1.   return (from offset in Enumerable.Range(64, Size)
  2.           //where offset <= RawContent.GetUpperBound(0) //if you want to, uncomment this line to avoid IndexOutOfBounds
  3.           select RawContent[offset]).ToArray();
  4.  
Jun 24 '10 #2
Airslash
221 100+
@ChBinder
thanks for the clarification.
I've currently worked around the issue by passing the same data array to the read function as a reference and add an additional parameter so it either overwriters the data or appends to it depending on the settings.

Thanks for the pointer on the size & offset. Added some checks to validate the size of the array....slipped my mind.
Jun 24 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
4
by: TomHL | last post by:
Hello, I want to send an additional info with byte array wiith sockets as "one packet". I know how to send the byte array by himself, but how can I send the additional info with it at the same...
5
by: Gerrit | last post by:
Hi all, I'm getting an OutOfMemoryException when I initialize a byte array in C# like this: Byte test = new Byte; I'm using ASP.NET 2.0. In ASP.Net 1.1 it works fine. So what am I doing
1
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.