Strpos handles the problem of binary flow

$buff = pack("nn",0xabcd,0x01);
$pos =  strpos($buff,0x01);
echo $pos;

3,0xabcd0x010,1,22
Php
Mar.07,2021

pack parameter n means 16bit unsigned short, big endian ((network byte order)
so the result of pack is 4 bytes (16 + 16bit), ord look at $buff [0], [1], [2], [3] are 171,205,0,1
0x01, according to ushort, the offset of 0x0001
strpos should also be 3

.

171 = 16 * 10 (a) + 11 (b)
205 = 16 * 12 (c) + 13 (d)

by the way: if pack uses S parameter, unsigned short (always 16 bit, machine byte order), our commonly used X86 CPU is little endian, so the result of
$buff should be 205, 171, 1, 0
then offset is 2

.

the core is the problem of byte order

Menu