Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-53706

QString::number(-17, 16) should return "-11" not "0xffffffffffffffef"



    • Story Points:
    • Commits:
      98666c8afc80cccb80ca4426b97ec52916c6e610 (qt/qtbase/dev)
    • Sprint:
      Qt6_Foundation_Sprint 22


      QLocaleData::longLongToString() suppresses AlwaysShowSign and BlankBeforePositive for bases other than ten on grounds "these are not supported by sprintf for octal and hex"; and it suppresses handling of negative (casting to unsigned, so that -17 becomes 0xffffffffffffffef) on the same grounds.

      I contend that this is entirely bogus behaviour.

      The sprintf reference (which could as well be to any member of the whole printf family) can only be to the fact that the o, u, x and X formats don't handle sign; these formats are defined to take unsigned inputs. They have no analogue for signed inputs, but that just means they provide no precedent for us to follow when we are handling a signed input. Our implementation of any printf-like function should, when processing %o, %u, %x or %X, use QLocaleData::unsLongLongToString(), so not be affected by what we chose to implement in QLocaleData::longLongToString() as handling of sign-related formatting details for bases other than ten. (Indeed, QString::vasprintf() does call the right method here, as it should.)

      Since QString::number(value, base=10) accepts an optional base, we should Do The Right Thing when the value is negative, handling sign gracefully, not casting via unsigned long long.

      In python, where there is no difference between signed and unsigned (only between ordinary int and infinite precision long - and even this makes no difference to string formatting), the %x format specifier cheerfully handles negative integers just fine, turning '%x' % -17 into '-11' as one would expect. This is clearly correct behaviour and a far more apt precedent for the case in hand.

      I propose, therefore, that QLocaleData::longLongToString() should discard its code that borks negative input for bases other than ten; and QString::number() would then handle negative numbers sanely with base.


          Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.



              andreasbuhr Andreas Buhr
              Eddy Edward Welbourne
              PM Owner:
              Maurice Kalinowski Maurice Kalinowski
              RnD Owner:
              Alex Blasche Alex Blasche
              1 Vote for this issue
              4 Start watching this issue



                  Gerrit Reviews