Byte problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Im a newbie and i need help.. would appreciate any help at all...

I need to make a method that accepts a 34 bytes data and inside the method i
need to get the 2nd, 3rd, 4th or any of the byte needed.. Is there an easy
wasy to do this? Ex. i need the 4th byte of the 34 bytes given.. i have no
idea at all how to do this.. please any help would do.. thanks!
 
The classic route would be an array:

public void SomeMethod(byte[] data) {
// paranoia
if(data==null) throw new ArgumentNullException("data");
// even more paranoia (ditch this if overkill)
if(data.Length != 34) throw new ArgumentException("Expected 34
bytes", "data");
// get 4th byte
byte fourth = data[3]; // zero-based
// do something fun
}

Marc
 
Thanks Marc, i didnt think it would be this simple. My boss told me about
stuff like placing it in a structure so it would be easier and more managed,
if anyone knows about this i would really appreciate it. Thanks marc btw, it
was a great answer.
 
Hi Rain,

If your bytes are made up of various data types, feeding it to a struct is
not too hard. Take a look at the article below, and particularly the
"Structs from streams" section.

[Mastering structs in C#]
http://www.vsj.co.uk/articles/display.asp?id=501


Thanks Marc, i didnt think it would be this simple. My boss told me about
stuff like placing it in a structure so it would be easier and more
managed,
if anyone knows about this i would really appreciate it. Thanks marc
btw, it
was a great answer.

Marc Gravell said:
The classic route would be an array:

public void SomeMethod(byte[] data) {
// paranoia
if(data==null) throw new ArgumentNullException("data");
// even more paranoia (ditch this if overkill)
if(data.Length != 34) throw new ArgumentException("Expected 34
bytes", "data");
// get 4th byte
byte fourth = data[3]; // zero-based
// do something fun
}

Marc
 
Byte is byte why need to put into structure? Is you boss asking you to build
a collection like stack, queue and so?

chanmm

Rain said:
Thanks Marc, i didnt think it would be this simple. My boss told me about
stuff like placing it in a structure so it would be easier and more
managed,
if anyone knows about this i would really appreciate it. Thanks marc btw,
it
was a great answer.

Marc Gravell said:
The classic route would be an array:

public void SomeMethod(byte[] data) {
// paranoia
if(data==null) throw new ArgumentNullException("data");
// even more paranoia (ditch this if overkill)
if(data.Length != 34) throw new ArgumentException("Expected 34
bytes", "data");
// get 4th byte
byte fourth = data[3]; // zero-based
// do something fun
}

Marc
 
if you put the data into a byte array then you could just access it using an
arrayindex.

byte[] buffer = new byte[2048];

buffer[0] would be the first byte

buffer[1] would be the second byte

....etc
 
Back
Top