bug fixed with negative BCD values

This commit is contained in:
2018-06-18 17:03:18 +02:00
parent 18a0e54033
commit 9481b95b30

View File

@ -44,50 +44,51 @@ printLong(const StreamFormat& fmt, StreamBuffer& output, long value)
ssize_t i; ssize_t i;
unsigned long prec = fmt.prec < 0 ? 2 * sizeof(value) : fmt.prec; // number of nibbles unsigned long prec = fmt.prec < 0 ? 2 * sizeof(value) : fmt.prec; // number of nibbles
unsigned long width = (prec + (fmt.flags & sign_flag ? 2 : 1)) / 2; unsigned long width = (prec + (fmt.flags & sign_flag ? 2 : 1)) / 2;
unsigned long val = value;
if (fmt.width > width) width = fmt.width; if (fmt.width > width) width = fmt.width;
if (fmt.flags & sign_flag && value < 0 && prec > 0) if (fmt.flags & sign_flag && value < 0 && prec > 0)
{ {
neg = true; neg = true;
value = -value; val = -value;
} }
output.append('\0', width);
if (fmt.flags & alt_flag) if (fmt.flags & alt_flag)
{ {
// least significant byte first (little endian) // least significant byte first (little endian)
i = -width;
while (width && prec) while (width && prec)
{ {
width--; width--;
bcd = value%10; bcd = val%10;
if (--prec) if (--prec)
{ {
--prec; --prec;
value /= 10; val /= 10;
bcd |= (value%10)<<4; bcd |= (val%10)<<4;
value /= 10; val /= 10;
} }
output.append(bcd); output[i++] = bcd;
} }
if (width)
output.append('\0', width);
if (neg) output[-1] |= 0xf0; if (neg) output[-1] |= 0xf0;
} }
else else
{ {
// most significant byte first (big endian) // most significant byte first (big endian)
output.append('\0', width); if (neg) output[-(long)width] = 0xf0;
if (neg) output[-(long)width] |= 0xf0;
i = 0; i = 0;
while (width && prec) while (width && prec)
{ {
width--; width--;
bcd = value%10; bcd = val%10;
if (--prec) if (--prec)
{ {
--prec; --prec;
value /= 10; val /= 10;
bcd |= (value%10)<<4; bcd |= (val%10)<<4;
value /= 10; val /= 10;
} }
output[--i]=bcd; output[--i] |= bcd;
} }
} }
return true; return true;