> On 20 Sep 2016, at 16:42, Makarius <makar...@sketis.net> wrote:
> On 20/09/16 16:50, Rob Arthan wrote:
>> I think this patch fixes it:
>> diff --git a/libpolyml/pexport.cpp b/libpolyml/pexport.cpp
>> index b03b1da..a9ebd2e 100644
>> --- a/libpolyml/pexport.cpp
>> +++ b/libpolyml/pexport.cpp
>> @@ -158,7 +158,7 @@ void PExport::printObject(PolyObject *p)
>> for (unsigned i = 0; i < ps->length; i++)
>> char ch = ps->chars[i];
>> - fprintf(exportFile, "%02x", ch);
>> + fprintf(exportFile, "%02x", ch & 0xff);
> It seems to work, but it is unclear to me why.
char can be either signed or unsigned. When passed to fprintf, it will
be promoted to either int or unsigned int (based on char’s signedness).
Thus, if char is signed, and ch is 0xf0 (for example), with a 32-bit int
it will be sign-extended to 0xfffffff0, and printed as fffffff0, rather
than f0. Then whatever is reading it will think that’s 4 bytes (3 ff’s
and 1 f0). By anding with 0xff, ch gets promoted to int, but then only
the lowest byte is selected, so it will stay as 0xf0.
Perhaps a clearer fix would be to make ch an unsigned char (or cast)?
polyml mailing list