Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [BUG]Conversion from signed number to hexadecimal string?
Forum Updated to NodeBB v4.3 + New Features

[BUG]Conversion from signed number to hexadecimal string?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 317 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adonezid
    wrote on last edited by
    #1
    int value = -1;
    QString str;
    str = QString::number(value, 16); // expect "ffffffff" got "ffffffffffffffff"
    short value2 = -32768;
    str = QString("%1").arg(value2, 0, 16); //expect "8000" got "ffffffffffff8000"
    

    Why do Qt automatically perform 64bit sign extension before convert to string? Is this a bug?

    It is very weird that you got string representation of 64 bit number while all you have is a byte even though they have same meaning then need to chop off the prefix

    str = QString("%1").arg(value, 0, 16).right(sizeof(value)<<1);
    
    Paul ColbyP 1 Reply Last reply
    0
    • A adonezid
      int value = -1;
      QString str;
      str = QString::number(value, 16); // expect "ffffffff" got "ffffffffffffffff"
      short value2 = -32768;
      str = QString("%1").arg(value2, 0, 16); //expect "8000" got "ffffffffffff8000"
      

      Why do Qt automatically perform 64bit sign extension before convert to string? Is this a bug?

      It is very weird that you got string representation of 64 bit number while all you have is a byte even though they have same meaning then need to chop off the prefix

      str = QString("%1").arg(value, 0, 16).right(sizeof(value)<<1);
      
      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @adonezid,

      int value = -1;
      QString str;
      str = QString::number(value, 16); // expect "ffffffff" got "ffffffffffffffff"

      I would expect str to be "-1", which I get with Qt 6.5, but with Qt 5.15 I see the same surprising behaviour as you reported. Looks like it was fixed in Qt 6.0 as part of QTBUG-53706.

      Cheers.

      JonBJ 1 Reply Last reply
      1
      • Paul ColbyP Paul Colby

        Hi @adonezid,

        int value = -1;
        QString str;
        str = QString::number(value, 16); // expect "ffffffff" got "ffffffffffffffff"

        I would expect str to be "-1", which I get with Qt 6.5, but with Qt 5.15 I see the same surprising behaviour as you reported. Looks like it was fixed in Qt 6.0 as part of QTBUG-53706.

        Cheers.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Paul-Colby
        Yep, that report describes the old behaviour as

        when the value is negative, handling sign gracefully, not casting via unsigned long long.

        Hence behaviour from OP. Looks like there was quite a bit of "differences of opinion" there :)

        Christian EhrlicherC 1 Reply Last reply
        0
        • JonBJ JonB

          @Paul-Colby
          Yep, that report describes the old behaviour as

          when the value is negative, handling sign gracefully, not casting via unsigned long long.

          Hence behaviour from OP. Looks like there was quite a bit of "differences of opinion" there :)

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          See e.g. https://bugreports.qt.io/browse/QTBUG-81379

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved