1

Trying to pass hex values through a form using php to be sent using TCP sockets like below

<html>
<body>
<form action="fsock.php" method="post">
HEX: <input type="text" name="hex"><br>

<input type="submit">
</form>

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

$hex = strToHex($_POST["hex"]);

$fp = fsockopen("192.168.1.108", 48879);
if (!$fp) {
    echo "Unable to open\n";
} else {

$in1 = "\x00\x07\x04";
$in2 = "\x01\x07\x01";

    fwrite($fp, $in1);
    fwrite($fp, $in2);
    fwrite($fp, $hex);
    stream_set_timeout($fp, 1);
    $res = fread($fp, 2000);

    $info = stream_get_meta_data($fp);
    fclose($fp);

    if ($info['timed_out']) {
        echo 'Connection timed out!';
    } else {
        echo $res;
    }
}
?>
</body>
</html>

How do I get $_POST["ex"] to be formatted as $in1 and $in2?

I can't get this to work with the tips below!

I use the following as input \x01\x07\x01 but I only get every single character converted to hex, not byte wise.

I want to have exactly the same output as $in2, how?

0 Answers0