PHP byte sequence for integers on big end machines

If I were to run this PHP code on a machine with a large entic, what would I get?

$v = 0x12345678;
for ($i = 0 ; $i < 4 ; $i++) {
    echo "$i: " . dechex(($v>>($i*8))&0xff) . "\n";
}

      

would be "A":

0: 78
1: 56
2: 34
3: 12

      

or it will be "B":

0: 12
1: 34
2: 56
3: 78

      

I ask how while there is almost always little endian on desktops, and PHP always uses the internal hardware representation (one of the most problematic flaws), it still prints "A" which is essentially a big argument for what I remember.

+3


source to share


1 answer


First of all, I don't have a big testing machine. Bert is based on theory if we plan $ v = 0x12345678; in memory then in littel endian it becomes:

byte0: 78 Byte 1: 56 byte2: 34 Byte 3: 12

But in big endian it becomes:



byte0: 12 byte1: 34 byte2: 56 byte3: 78

So, you should see A in the small endian and B in the big endian.

Also, if you want to simulate the behavior of the bigandians, you can just try the htons function, which converts a short number from host architecture to network byte order and is always a big fan of it.

0


source







All Articles