1

I need to get specific bytes from a byte array. I know the content of the first byte I want and I need x bytes after that.

For example, I have

byte [] readbuffer { 0, 1, 2, 0x55, 3, 4, 5, 6};
byte [] results = new byte[30];

and I need the 3 bytes that appear after "0x55"

byte results == {ox55aa, 3, 4, 5}

I'm using:

Array.copy(readbuffer, "need the index of 0x55" ,results, 0, 3);

I need to find the index of 0x55

PS: 0x55 is in an aleatory position in the array. PS2: I forgot to mention before that I'm working in .Net Micro Framework.

(I'm sorry for the non code description, I'm a very newbie to programming... and english)

thank you in advance

[edited]x2

Jónatas Brás
  • 72
  • 1
  • 2
  • 10
  • Your question is really unclear... it would be *much* easier to help you if you would give an example in code, rather than trying to describe it. (0x55aa isn't a valid byte value, so it can't appear in the array...) – Jon Skeet Feb 13 '15 at 12:40
  • Are you looking for the BinaryReader class? You can set the starting byte number to read from – Val Feb 13 '15 at 12:41
  • 1
    @Jon Skeet You're right , it's just 0x55 (I confused this because I am really trying to find 0x55 and 0xaa, in my project). – Jónatas Brás Feb 13 '15 at 14:08

3 Answers3

5

This can be achieved like this:

byte[] bytes = new byte[] { 1, 2, 3, 4, 0x55, 6, 7, 8 };
byte[] newBytes = new byte[4];
Buffer.BlockCopy(bytes,Array.IndexOf(bytes,(byte)0x55), newBytes,0,4);
Alex Voskresenskiy
  • 1,984
  • 2
  • 17
  • 28
  • 1
    I did not know BlockCopy, so I searched it and found, it's similar to Array.Copy, but works on byte-level. As he is using a byte array, it's okay here, but will fail on other arrays: http://stackoverflow.com/questions/1389821/array-copy-vs-buffer-blockcopy. Therefore I recommend Array.Copy. But in general, your solution is more elegant than mine. – Tobias Knauss Feb 13 '15 at 12:49
  • @TobiasKnauss, i used BlockCopy for other array types with some code changes and it us much more efficient than Array.Copy (cannot provide links as i worked on this long time ago). Anyway, the initial question uses bytes, so blockcopy is cool. – Alex Voskresenskiy Feb 13 '15 at 12:52
  • @TobiasKnauss forgot to make a cast in IndexOf, fixed my error – Alex Voskresenskiy Feb 13 '15 at 12:52
  • 1
    probably not more efficient: http://www.codeproject.com/Articles/65183/Buffer-BlockCopy-not-as-fast-as-you-think. I did not use it, so I can only refer to other docs. I found that one on my first search for BlockCopy. – Tobias Knauss Feb 13 '15 at 12:57
  • Thank you for your answer. I forgot to mention I'm using .Net Micro Framework, sorry. I edited my question. – Jónatas Brás Feb 13 '15 at 15:56
2

I guess you simply need to search the whole array for the specific value and remember the index where you find it...

int iIndex = 0;  
for (; iIndex < valuearray.Length; iIndex++);
  if (valuearray[iIndex] == searchedValue) break;

and from here on do what you want with the found index.

P.S. maybe there are slight syntax failures, as I usually use C++.net

Tobias Knauss
  • 3,160
  • 1
  • 18
  • 41
  • Thank you. I tried it and it's not working. I forgot to mention I'm working in .Net Micro Framework. PS.:I edited my question. – Jónatas Brás Feb 13 '15 at 15:55
1
        byte[] results = new byte[16];
        int index = Array.IndexOf(readBuffer, (byte)0x55);
        Array.Copy(readBuffer, index, results, 0, 16);

Thank you all.

This is my code now. it's working as I expect :)

Jónatas Brás
  • 72
  • 1
  • 2
  • 10