P
P
Pavel2016-12-06 17:59:27
PHP
Pavel, 2016-12-06 17:59:27

How to create a null-terminated string in php?

To communicate with the device I use sockets and php.
The following code works fine: we take a null-terminated string and send it to the socket

$msg = "\x00\x00\x00\x00\x63\x03\x67";
socket_write ( $socket , $msg, strlen($msg));

But when trying to dynamically create such a string like this:
$data = "00000000630367";
$msg = "\x".implode("\x",str_split($data,2))

I get an ordinary string, and, accordingly, when sent to the device, it does not understand what I want.
How to correctly generate a request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
none7, 2016-12-06
@PavelFokeev

hex2bin

A
Andrew, 2016-12-06
@impwx

\x00is a sequence substituted by PHP at file compilation time, not at runtime.
If you want to get a character by its numeric code, there is a function:
php.net/manual/en/function.chr.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question