I'm reading through a library (github.com/adduc/phpmodbus) and there's this function for converting integer to little-endian or big-endian string of bytes:
private static function endianness($value, $endianness = 0) {
if ($endianness == 0)
return
self::iecBYTE(($value >> 8) & 0x000000FF) .
self::iecBYTE(($value & 0x000000FF)) .
self::iecBYTE(($value >> 24) & 0x000000FF) .
self::iecBYTE(($value >> 16) & 0x000000FF);
else
return
self::iecBYTE(($value >> 24) & 0x000000FF) .
self::iecBYTE(($value >> 16) & 0x000000FF) .
self::iecBYTE(($value >> 8) & 0x000000FF) .
self::iecBYTE(($value & 0x000000FF));
}
The iecBYTE
function is just chr($value & 0xFF)
.
Now maybe I'm thick, but the little-endian string looks wrong.
For example, with 0xAABBCCDD
, you'd get {CC}{DD}{AA}{BB}
.
I even looked it up on Wikipedia. Shouldn't it be {DD}{CC}{BB}{AA}
?
The code works though, which really confuses me. Is it right and I understand it incorrectly?