Byte problem

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!
 
M

Marc Gravell

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
 
G

Guest

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.
 
M

Morten Wennevik

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
 
C

chanmm

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
 
M

Michael Matteson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top