miseleigh Posted March 18, 2008 Share Posted March 18, 2008 I need to create strings with escape hex numbers - I'm trying to use sockets to write to a device that needs binary values. If I use a single string like "\x8" it does exactly what I'm looking for, but putting the 8 into a variable and appending them doesn't work properly. Any tips? $hex1 = "\x8"; socket_write($socket, $hex1); //this works! $hex = 8; $hex2 = "\x".$hex; socket_write($socket, $hex2); //this one doesn't, but I need to be able to do this. I've also tried $hex2 = '\x'.$hex and $hex2 = "\x$hex". Neither worked. Any help would be awesome. Link to comment https://forums.phpfreaks.com/topic/96740-escaping-characters-in-strings/ Share on other sites More sharing options...
JD* Posted March 18, 2008 Share Posted March 18, 2008 Try this: $hex1 = "\x8"; socket_write($socket, $hex1); //this works! $hex = "8"; $hex2 = "\x".$hex; socket_write($socket, $hex2); //this one doesn't, but I need to be able to do this. Link to comment https://forums.phpfreaks.com/topic/96740-escaping-characters-in-strings/#findComment-495057 Share on other sites More sharing options...
soycharliente Posted March 18, 2008 Share Posted March 18, 2008 What about? <?php $hex = 8; $hex2 = "\x" . strval($hex); socket_write($socket, $hex2); ?> Link to comment https://forums.phpfreaks.com/topic/96740-escaping-characters-in-strings/#findComment-495067 Share on other sites More sharing options...
miseleigh Posted March 18, 2008 Author Share Posted March 18, 2008 Thanks for your replies, but neither worked. I think the problem is that PHP is evaluating the \x part before the number is added to the string, so it doesn't realize it's supposed to escape it. Or something. Link to comment https://forums.phpfreaks.com/topic/96740-escaping-characters-in-strings/#findComment-495149 Share on other sites More sharing options...
effigy Posted March 18, 2008 Share Posted March 18, 2008 $hex2 = pack('h', $hex); Link to comment https://forums.phpfreaks.com/topic/96740-escaping-characters-in-strings/#findComment-495158 Share on other sites More sharing options...
miseleigh Posted March 20, 2008 Author Share Posted March 20, 2008 Thank you effigy, that is working perfectly. Link to comment https://forums.phpfreaks.com/topic/96740-escaping-characters-in-strings/#findComment-496965 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.