Skip to content
tpkarras edited this page May 13, 2025 · 7 revisions

Octets are numbers containing a binary sequence that indicates either the number or a continuation of said sequence.

Each octet is an 8 bit string, bits 1-8 can be either 1 or 0, bit 8 indicates whether it's a continuation (1) or a terminus (0).

Octets must be 0-128 or 255, examples are provided below.

  1. The number 0. 00000000
  2. The number 127. 11111110
  3. The number 128. 10000000
  4. The number 255. 11111111

You will have to create a function to parse numbers into a series of octets. As mentioned before, octets must have a length number containing the number of octets preceding the content of octets.

An example function is provided below.

$length = 0;
while(true){
if(!isset($number)){
$number = $element;
if($number > 128){
if(is_string($length)){
$length .= hex2bin(dechex(255));
} else {
$current_octet .= hex2bin(dechex(255));
}
$number = $number - 128;
} else {
$number = dechex($number);
if(strlen($number) & 1){
$number = "0".$number;
}
if(is_string($length)){
$length .= hex2bin($number);
$current_octet = $length.$current_octet;
$length = null;
$octets .= $current_octet;
$current_octet = "";
unset($number);
break;
} else {
$current_octet .= hex2bin($number);
}
}
if(!is_string($length)){
$length++;
}
if(is_string($number)){
if(isset($key) && isset($element[$key + 1])){
$key++;
$number = $element[$key];
continue;
} else if($length > 0){
$number = $length;
$length = "";
continue;
}
}
}
}

An example function to parse octets is prov

$data = "";
$current_position = 0;
while(true){
$number = hexdec(bin2hex(substr($data, $current_position, 1)));
$current_position++;
if($number === 255){
$element = $element + ($number >> 1) + 1;
} else if($number <= 128){
$element = $element + $number;
} else if ($number > 128){
//return exception;
}
}

It is ultimately up to you on how to implement the octet creating/parsing function.

Clone this wiki locally