php - read single byte as an unsigned byte

Solution:
The unpack format string is actually likecode [repeater] [name]
, separated by forward slashes. For example,Clength
. The output array will be associative and keyed by name, with repeated fields having a numeric suffix appended, starting at 1. E.g. the output keys forC2length
will belength1
andlength2
.
The documentation is not super-clear about this.
So when you don't specify any name, it just appends the numeric suffix, starting at 1. So the length you are looking for is$lenarr[1]
.
But you should try this instead:
$v = unpack('Clength', "\x04");
var_export($v); // array('length' => 4)
Here are some other examples:
unpack('C2length', "\x04\x03");
// array ( 'length1' => 4, 'length2' => 3, );
unpack('Clength/Cwidth', "\x04\x03");
// array ( 'length' => 4, 'width' => 3, );
Also, in php you can't generally use array-access notation on an expression--you need to use it on the variable directly. Sofunctioncall()[0]
won't work, you need$v = functioncall(); $v[0]
. This is a php wart.
Answer
Solution:
You can't access a returned array like that. You'd do$len[0]
after callingunpack()
.
$len = unpack("C",socket_read($socket, 1));
echo "after " . $len[0] . $endl;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.