I don't think that an error needs to be thrown, as it will just overflow and wrap around. My concern is that using %c may or may not throw a sign bit into the encoded value. That requires the recipient of that value to know whether the value was negative ahead of time.
Or, put another way, if you encode a 32 bit number using %4c, how do you decode it reliably?
Bill
On Wed, 22 Jun 2011, Mirar @ Pike developers forum wrote:
A binary number is a binary number regardless, so I don't think it should do anything in sprintf. Did you want it to throw an error if the number doesn't fit?
These integers will have the same lowest bits, regardless:
sprintf("%1c",-2);
(13) Result: "\376"
sprintf("%1c",254);
(14) Result: "\376"
sprintf("%1c",254+65536);
(15) Result: "\376"
It seems '+' to sscanf means to read it as a signed integer, so for me it works as expected:
array_sscanf("\xff\xfe"*2,"%2c%-2c");
(23) Result: ({ /* 2 elements */ 65534, 65279 })
array_sscanf("\xff\xfe"*2,"%+2c%+-2c");
(24) Result: ({ /* 2 elements */ -2, -257 })