开发者

PHP Create C string

开发者 https://www.devze.com 2023-04-03 10:41 出处:网络
I need to pass messages to my C program from PHP and I am doing this via message queues. I have the message queues working and both sides c开发者_JAVA百科an receive messages.

I need to pass messages to my C program from PHP and I am doing this via message queues.

I have the message queues working and both sides c开发者_JAVA百科an receive messages.

The problem is on the php side formatting the data. I am trying to send C style string, but php handles strings much differently. How would I convert the php string into a null temrinated C string?

Basically I need 'config1' to be the null terminated string.

msg_send($mq_id, $MSG_CHANGECONFIG, 'config1', true, false, $error);

It appears php stores strings like so: \"s:8:\\"config1\000\\"; where \ are just escapes.

Is there any way to do this, or a different way to parse this from the C side in order to convert it to a C string?


You can try with

$nullTerminatedString = sprintf("config1%c", 0);
// or directly using escape sequence
$nullTerminatedString = "config\0";
echo strlen(sprintf("config1%c", 0)); # returns 8, so it should work

Taken from here

serialize The optional serialize controls how the message is sent. serialize defaults to TRUE which means that the message is serialized using the same mechanism as the session module before being sent to the queue. This allows complex arrays and objects to be sent to other PHP scripts, or if you are using the WDDX serializer, to any WDDX compatible client.

So your call should be:

msg_send($mq_id, $MSG_CHANGECONFIG, 'config1', **false**, false, $error);


If you only want to add a null byte to it, use an octal escape sequence (you need to use double quotes for that):

"config1\0"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号