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

3
  • 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...) Commented Feb 13, 2015 at 12:40
  • Are you looking for the BinaryReader class? You can set the starting byte number to read from Commented Feb 13, 2015 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). Commented Feb 13, 2015 at 14:08

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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: stackoverflow.com/questions/1389821/…. Therefore I recommend Array.Copy. But in general, your solution is more elegant than mine.
@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.
@TobiasKnauss forgot to make a cast in IndexOf, fixed my error
probably not more efficient: codeproject.com/Articles/65183/…. I did not use it, so I can only refer to other docs. I found that one on my first search for BlockCopy.
Thank you for your answer. I forgot to mention I'm using .Net Micro Framework, sorry. I edited my question.
4
        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 :)

Comments

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

1 Comment

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.
-1
byte[] bufferMain = new byte[16];
int indexStart = 3;  // starting index of sub-array of bytes
int indexEnd = 8;    // end index of sub-array of bytes
var bufferSub = new ReadOnlySpan<byte>(bufferMain, indexStart, indexEnd - indexStart).ToArray();

1 Comment

Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. This is especially important when answering old questions (this one is nearly 8 years old) with existing answers. Please edit your answer and explain how it answers the specific question being asked, and how it improves upon the answers that were already here. See How to Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.