3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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, $testPart); //echo strToHex($testPart) . "<br>\n"; } $packed = pack('nvH*', '0x'.$testParts[0], '0x'.$testParts[1], '0x'.$testParts[2], '0x'.$testParts[3], '0x'.$testParts[4], '0x'.$testParts[5], '0x'.$testParts[6], '0x'.$testParts[7]); $unpacked = unpack('nvH*asdf', $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); ?>
Output for 4.3.2 - 7.1.0
Warning: pack(): 5 arguments unused in /in/MViqX on line 32 Warning: pack(): Type H: illegal hex digit x in /in/MViqX on line 32 string(7) "��" array(1) { ["vH*asdf"]=> int(0) } 0
Output for 4.3.0 - 4.3.1
Warning: pack() [http://www.php.net/function.pack]: 5 arguments unused in /in/MViqX on line 32 Warning: pack() [http://www.php.net/function.pack]: Type H: illegal hex digit x in /in/MViqX on line 32 string(7) "��" array(1) { ["vH*asdf"]=> int(0) } 0