Finding bytes in byte array - Visual Studio - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Finding bytes in byte array

micro framework .netmf method

  • Please log in to reply
4 replies to this topic

#1 JBkey

JBkey

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationAveiro, Portugal

Posted 12 February 2015 - 03:32 PM

Hi!
 
 
I need some advice, and I think this is the right place, if it isn't, let me know.
 
I'm very limited on my programming knowledge, so maybe this is an easy question to answer.
 
 
 
 
I have an array of bytes that contains values like this:
byte [] values {00, 22, 33, 44,  0x55, 55, 66, 77, 88, 99, 10}   
And I need to select a number x of bytes that appear after the 0x55 into another array (I only used hex to differenciate).
byte [x] selec_values { 0x55, 55, 66, 77, 88}
I thought I could search for that byte using "array.find" and I can't. Then I tried to build a list with the array values, and... again, I can't..
 
Does anyone know how I do that?
 
 
 
 
JB

Edited by Chris Walker, 12 February 2015 - 07:35 PM.
Removed duplicates, clarified post subject line


#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 13 February 2015 - 11:31 AM

Aveiro, Portugal! My wife was there once, and told me it's an awesome city!

 

As far I know, there's no any immediate way to achieve kinda "Array.find", but of course you can create it. If you were using the regular (i.e. desktop) .Net Framework, you could use Linq, which allows to "compose" whatever function you want with ease.

For the Netduino, you can use something like that:

 

                var source = new byte[] { 00, 22, 33, 44, 0x55, 55, 66, 77, 88, 99, 10 };
                var index = Array.IndexOf(source, 0x55);
                var targetLength = source.Length - index - 1;
                var target = new byte[targetLength];
                source.CopyTo(target, index + 1);

Hope it helps.

 

 

 


Biggest fault of Netduino? It runs by electricity.

#3 JBkey

JBkey

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationAveiro, Portugal

Posted 15 February 2015 - 09:15 AM

It's like a little venezia,  ;)



Before I saw your comment I reached the same solution. And it's working so far.


                int index = Array.IndexOf(readBuffer, "0x55");

                byte[] results = new byte[16];

                Array.Copy(readBuffer, index, results, 0, 16);                

But I forgot something...

In the source byte[] (readBuffer), I have more than one "0x55 block" and I need all of them (I didn't know this before, I thought it would be just one block per array).. and array.copy only returns the first one. Any ideas?

 

 

 

 

Jónatas Brás



#4 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 17 February 2015 - 05:16 AM

Hmm...but what do you mean as "the first one"? That is, what's the expect "end of block"?

Looks like you have to deal with a kind of pattern, and you have to extract all its occurrences...

 

Anyway, before knowing what's you answer, you may also consider another way to deal with such a situations. It may sound as an overkill, but when you have to deal with data extraction (in general), composition, and others function to/from a byte array, the below one is maybe very neat.

http://cetdevelop.co...eArrayReader.cs

(and related classes)

 

It's basically a bunch of classes for byte-array manipulation: reader, writer, data extraction and insertion, etc.

Please, note that the library is a bit "aged" (about two years), but should be working fine with the latest firmwares.

Cheers


Biggest fault of Netduino? It runs by electricity.

#5 JBkey

JBkey

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationAveiro, Portugal

Posted 17 February 2015 - 09:19 AM

Hi.

 

thank you for your answer.

 

 

You are right. I need to extract all the occurrences. About the end of block I know it's the 16th byte after the "0x55" occurrence.

 

 

I'm going to study your suggestion and I'll let you know what's my decision ;)

 

 

PS. : How was the carnival? I hear that Venezian carnival is awesome.







Also tagged with one or more of these keywords: micro framework, .netmf, method

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.