<?php
function strToHex($string)
{
    $hex = '';
    for ($i = 0; $i < strlen($string); $i++) {
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= '\\x' . substr('0' . $hexCode, -2);
    }
    return strToUpper($hex);
}
function hexToStr($hex)
{
    $string = '';
    for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
        $string .= chr(hexdec($hex[$i] . $hex[$i + 1]));
    }
    return $string;
}

// Tests
header('Content-Type: text/plain');
function letratohexa($palabra)
{
    $matriz1 = str_split($palabra);

    $stringlisto = '';

    foreach ($matriz1 as $key => $num) {
        echo ' letra:  ' . $num . ' hex ' . strToHex($num);
        echo "\n";

        echo hexdec(strToHex($num));

        $stringlisto = $stringlisto . ' ' . strToHex($num);
    }

    return $stringlisto;
}

echo 'listafinal ' . letratohexa('carlos');

echo "\n";

$cadena = "Hello Friend";

$matriz1 = str_split($cadena);
$matriz2 = str_split($cadena, 5);

print_r($matriz1);
print_r($matriz2);

$stringlisto = '';

foreach ($matriz1 as $key => $num) {
    echo ' letra:  ' . $num . ' hex ' . strToHex($num);
    echo "\n";

    $stringlisto = $stringlisto . ' ' . strToHex($num);
}
echo 'listado ' . $stringlisto;

echo "\n";
$success = true;
echo strToHex('carlos');
echo "\n";
echo strToHex(hexToStr('FF'));
echo "\n";
echo strToHex(hexToStr('000102FF'));
echo "\n";
echo 'va listo: ' . hexToStr("0204001b062100");
echo "\n";
echo hexdec(63);
?>