how would I convert an integer to an array of 4 bytes?
Here is the exact code I want to port (in C#)
int i = 123456;
byte[] ar = BitConverter.GetBytes(i);
// ar will contain {64, 226, 1, 0}
How would I do the exact same thing in PHP ?
how would I convert an integer to an array of 4 bytes?
Here is the exact code I want to port (in C#)
int i = 123456;
byte[] ar = BitConverter.GetBytes(i);
// ar will contain {64, 226, 1, 0}
How would I do the exact same thing in PHP ?
Since the equivalent of a byte array in PHP is a string, this'll do:
To visualize that, use
bin2hex
:The equivalent conversion is
See it in action.
You should be aware though that the byte order (little/big endian) is dependent on the machine architecture (as it is also in the case of
BitConverter
). That might or might not be good.