473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting array data

1 New Member
I'm trying to figure out the best approach to grab data from an array. So heres the deal. I have byte array called curveData[256] filled will hex bytes that I've downloaded from a unit under test. Basically, I am trying to loop through this array of 256 bytes. I want to grab the 1st byte (of a 4 byte chunk), so [0], then [4] [8] and so on…so every 4th byte starting with the first and throwing them into the rpmArray. Then I want to grab the bytes in between, [1][2][3] …etc, and throw them into another array called multOffArray. Here is my code below

|0|1|2|3|4|5|6|7|…………|252|253|254|255|


int i;
byte[] rpmArray = new byte[64];
byte[] multOffArray = new byte[192];
for (i = 0; i < curveData.Length; i++ )
{
if (i%4 == 0 || i%4 == 4)
Array.Copy(curveData, curveData[i], rpmArray, curveData[i], 1);
else
Array.Copy(curveData, curveData[i], multOffArray, curveData[i+1], 3);
}
DisplayData(MessageType.Outgoing, "\n rpmArray: " + rpmArray);
DisplayData(MessageType.Outgoing, "\n multOffArray: " + multOffArray);

I'm getting errors saying that the index is out of bounds. When I check the bytes in the arrays where I’m storing this info, its not in the order that I anticipated and I am missing data. I tried to set it up so that at every index which is a multiple of 4, would initiate copying of data from the source array to my destination array. Maybe somebody can help me get this loop right. It's been a while since I've written code and I know this isn't right. If someone can help it'd be much appreciated.
Oct 8 '08 #1
3 1225
balabaster
797 Recognized Expert Contributor
Would something like this be suitable?

Expand|Select|Wrap|Line Numbers
  1. System.Collections.ArrayList rpmArray = new  System.Collections.ArrayList();
  2. System.Collections.ArrayList multOffArray = new System.Collections.ArrayList();
  3.  
  4. for (int i = 0; i < curveData.Length; i++)
  5. {
  6.     if (i % 4 == 0)
  7.         rpmArray.Add(curveData[i]);
  8.     else
  9.         multOffArray.Add(curveData[i]);
  10. }
It bypasses the need for array management and if you want to treat the ArrayLists as arrays, just reference them by rpmArray.ToArray() and multOffArray.ToArray()
Oct 9 '08 #2
balabaster
797 Recognized Expert Contributor
Or you could use the snazzy new LINQ to query the data set rather than iterating through it:

Expand|Select|Wrap|Line Numbers
  1. int rowIndex = 0;
  2.  
  3. var rpmQry = 
  4.   from item in (
  5.     from value in curveData 
  6.     select new {index = rowIndex++, value}
  7.   )
  8.   where item.index % 4 == 0
  9.   select item.value;
  10.  
  11. var rpmData = rpmQry.ToArray();
I tried to figure out a more succinct way of getting at the row indexes of each item to query for mod 4, but it's late and I'm tired... it would be nicer as a simple query rather than nested query. I started off with two queries, but I didn't like that it was being visited twice, until I wanted to reference it a second time for the multOffArray:

Expand|Select|Wrap|Line Numbers
  1. int rowIndex = 0;
  2. var allDataQry = from item in curveData select new { index = rowIndex++, value = item };
  3. var rpmArray = (from item in allDataQry where item.index % 4 == 0 select item.value).ToArray();
  4. var multOffArray = (from item in allDataQry where item.index % 4 != 0 select item.value).ToArray();
  5.  
Oct 9 '08 #3
Plater
7,872 Recognized Expert Expert
As for why you get index of bounds:
Think about what happens on the last itteration for i, i would be the last index in your array. So what would curveData[i+1] (Note: i+1) be, out of bounds.
Oct 9 '08 #4

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

Similar topics

7
3233
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
0
2641
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
4
2519
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
5
2501
by: DKC | last post by:
Hi, Using VB.NET. I have a datagrid having a strongly typed array of objects as its data source. The data from the array of objects is displayed by means of a table style, which is fine, but...
0
3214
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
7
4796
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
5
6667
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
7166
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
6
4050
by: =?Utf-8?B?RGFu?= | last post by:
I am reposting a question from about 3 weeks ago ("sorting capability"). I have an aspx page in which I get the data from a database dynamically, through C# code, by creating a dynamic table...
5
4902
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
7072
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
5570
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,...
1
4998
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
4666
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...
0
3160
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...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
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...

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.