我很費勁地向連接到我的PHP套接字服務器的設備發送十六進制值。在PHP中以十六進制形式發送
我有這樣的代碼:
<?PHP
# 7e hex = 126 decimal
$str1 = "\x7e\x00MyData";
sendToDevice($str1); // works :-)
# will send "~<NUL>MyData" and the device sends expected result back
$dec = 126;
$hex = dechex($dec);
$str2 = $hex . "\x00MyData";
sendToDevice($str2); // does not work :-/
# will send "7eMyData" and the device sends nothing back
$dec = 126;
$hex = dechex($dec);
$str3 = "\x$hex\x00MyData";
sendToDevice($str3); // does not work :-/
# will send "\x7e<NUL>MyData" and the device sends error report back
?>
我怎麼能送它,所以它的工作原理與$ STR1?
你是什麼意思的「不工作: - /」?你有錯誤嗎? – Dahaka
這是我注意到的東西。當用雙引號包裝時,PHP似乎評估十六進制數字。例如,'print_r(「\ x7f \ x00MyData」);'和'print_r('\ x7f \ x00MyData');'的輸出是不一樣的。也許這會幫助你。另請參閱http://codepad.org/GbbhYouP。 – xbonez
請添加'sendToDevice()'的源代碼。否則,它只是猜測 – hek2mgl