3v4l.org

run code in 300+ PHP versions simultaneously
<?php function strToHex($string) { $hex=''; for ($i=0; $i < strlen($string); $i++) { $hex .= dechex(ord($string[$i])); } return $hex; } function hexToStr($hex) { $string=''; for ($i=0; $i < strlen($hex)-1; $i+=2) { $string .= chr(hexdec($hex[$i].$hex[$i+1])); } return $string; } $test = "ffff:eeee:dddd:cccc:bbbb:aaaa:9999:8888"; $testParts = explode(':', $test); $testHex = array(); foreach($testParts as $testPart){ array_push($testHex, strToHex($testPart)); echo strToHex($testPart) . "<br>\n"; } $packed = pack('nvc*', $testHex[0], $testHex[1], $testHex[2], $testHex[3], $testHex[4], $testHex[5], $testHex[6], $testHex[7]); $unpacked = unpack('nvc*', $packed); $unpackedString = array(); var_dump($testParts); var_dump($testHex); var_dump($packed); var_dump($unpacked); foreach($unpacked as $unpack){ array_push($unpackedString, $unpack); } echo implode(':', $unpackedString); ?>

preferences:
37.54 ms | 402 KiB | 5 Q