-
Notifications
You must be signed in to change notification settings - Fork 0
Octets
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.
- The number 0.
00000000 - The number 127.
11111110 - The number 128.
10000000 - 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 provided below.
It is ultimately up to you on how to implement the octet creating/parsing function.