Question

I have application that play Pcap files and i try to add function that wrap my packet with PPPOE layer.

so almost all done except large packets that i didn't understand yet how to set the new langth after add PPPOE layer.

For example this packet:

enter image description here

As you can see this packet length is 972 bytes (03 cc), and all i want is to convert it to decimal, after see this packet byte[] in my code i can see that this value converted into 3 and 204 in my packet byte[], so my question is how this calculation works ?

Was it helpful?

Solution

Those two bytes represents a short (System.Int16) in bigendian notation (most significant byte first).

You can follow two approaches to get the decimal value of those two bytes. One is with the BitConverter class, the other is by doing the calculation your self.

BitConverter

// the bytes
var bytes = new byte[] {3, 204};
// are the bytes little endian?
var littleEndian = false;  // no

// What architecure is the BitConverter running on?
if (BitConverter.IsLittleEndian != littleEndian) 
{
   // reverse the bytes if endianess mismatch
   bytes = bytes.Reverse().ToArray();
}
// convert
var value =  BitConverter.ToInt16( bytes , 0);
value.Dump(); // or Console.WriteLine(value); --> 972

Calculate your self

base 256 of two bytes:

// the bytes
var bytes2 = new byte[] {3, 204};
// [0] * 256 + [1]
var value2 = bytes2[0] * 256 + bytes2[1];  // 3 * 256 + 204
value2.Dump();  // 972
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top