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. QByteArray toLong false
Qt 6.11 is out! See what's new in the release blog

QByteArray toLong false

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 9 Posters 1.3k Views 5 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    if it begins with "0", it is assumed to be octal

    So QByteArray thinks the number is octal but finds characters inside... maybe that's the reason for the crash. https://doc.qt.io/qt-5/qbytearray.html#toLong

    In any case, it obviously should not crash, but rather return ok == false. Report it to Qt bugtracker, please. https://bugreports.qt.io/secure/Dashboard.jspa

    (Z(:^

    Pl45m4P 1 Reply Last reply
    2
    • sierdzioS sierdzio

      if it begins with "0", it is assumed to be octal

      So QByteArray thinks the number is octal but finds characters inside... maybe that's the reason for the crash. https://doc.qt.io/qt-5/qbytearray.html#toLong

      In any case, it obviously should not crash, but rather return ok == false. Report it to Qt bugtracker, please. https://bugreports.qt.io/secure/Dashboard.jspa

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #3

      @sierdzio

      I thought the same at first, but the "16" indicates that it should get interpreted as hex (base 16).


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by
        #4

        Well, I don't know what you're expecting... AFAIK long is at most 64 bits, therefore any hex string longer than "0x8000000000000000" won't give you anything meaningful. Apparently python's way is different, but this number is way to large to be handled properly using C++ native integer types.

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

        Pl45m4P 1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #5

          Sure, the conversion should fail. But it should not crash.

          (Z(:^

          J.HilkJ 1 Reply Last reply
          3
          • sierdzioS sierdzio

            Sure, the conversion should fail. But it should not crash.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @sierdzio said in QByteArray toLong crash:

            Sure, the conversion should fail. But it should not crash.

            and it doesn't, at least in my test case:

            int main (int argc, char *argv[])
            {
            
                QApplication app(argc, argv);
            
            
                QByteArray a= "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
                bool ok;
                long la = a.toLong(&ok, 16);
                qDebug() << a << ok;
                return  la;
            }
            

            88c5d7c0-3efd-42d6-8d3b-670529dc099d-image.png


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            4
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #7

              Hm, interesting. So, @sonichy either you have discovered a crash on some specific compiler or platform, or the crash you see comes from some other part of your code.

              (Z(:^

              aha_1980A Pablo J. RoginaP 2 Replies Last reply
              3
              • sierdzioS sierdzio

                Hm, interesting. So, @sonichy either you have discovered a crash on some specific compiler or platform, or the crash you see comes from some other part of your code.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @sierdzio said in QByteArray toLong crash:

                or the crash you see comes from some other part of your code.

                I'm pretty sure :)

                Btw, does not crash on Linux with Qt 5.14.1

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                3
                • sierdzioS sierdzio

                  Hm, interesting. So, @sonichy either you have discovered a crash on some specific compiler or platform, or the crash you see comes from some other part of your code.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by Pablo J. Rogina
                  #9

                  @sonichy said in QByteArray toLong crash:

                  or the crash you see

                  What crash? Please post the actual stack trace

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  1
                  • JohanSoloJ JohanSolo

                    Well, I don't know what you're expecting... AFAIK long is at most 64 bits, therefore any hex string longer than "0x8000000000000000" won't give you anything meaningful. Apparently python's way is different, but this number is way to large to be handled properly using C++ native integer types.

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #10

                    @JohanSolo said in QByteArray toLong crash:

                    Apparently python's way is different

                    Seems like Python has an unlimited long int precision,

                    Plain integers (also just called integers) are implemented using long in C, which gives them at least 32 bits of precision. Long integers have unlimited precision. Floating point numbers are implemented using double in C.

                    (from: https://docs.python.org/2.4/lib/typesnumeric.html)

                    while C++ doesn't have it (of course). Usually 16b, 32b, 64b for short, int, long.

                    @sonichy
                    So you can't process your large byte array as single value in C++.

                    @J-Hilk
                    Haven't tested myself, but does your long really equal the value of that byte array, or is qDebug() just printing an array of hex chars?
                    Nevermind, I'm an idiot :D You do print that QByteArray , not the actual long int... :)

                    Edit: Interesting, that you ( @J-Hilk ) return la and the value is 0. The null could be the reason for the crash, @sonichy is facing. Depends on where and how that long is used later in the code.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    JonBJ 1 Reply Last reply
                    1
                    • Pl45m4P Pl45m4

                      @JohanSolo said in QByteArray toLong crash:

                      Apparently python's way is different

                      Seems like Python has an unlimited long int precision,

                      Plain integers (also just called integers) are implemented using long in C, which gives them at least 32 bits of precision. Long integers have unlimited precision. Floating point numbers are implemented using double in C.

                      (from: https://docs.python.org/2.4/lib/typesnumeric.html)

                      while C++ doesn't have it (of course). Usually 16b, 32b, 64b for short, int, long.

                      @sonichy
                      So you can't process your large byte array as single value in C++.

                      @J-Hilk
                      Haven't tested myself, but does your long really equal the value of that byte array, or is qDebug() just printing an array of hex chars?
                      Nevermind, I'm an idiot :D You do print that QByteArray , not the actual long int... :)

                      Edit: Interesting, that you ( @J-Hilk ) return la and the value is 0. The null could be the reason for the crash, @sonichy is facing. Depends on where and how that long is used later in the code.

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

                      @Pl45m4 said in QByteArray toLong crash:

                      Edit: Interesting, that you ( @J-Hilk ) return la and the value is 0. The null could be the reason for the crash, @sonichy is facing. Depends on where and how that long is used later in the code.

                      ? Even if the value is 0 (I don't know whether it is or not), it's a number, zero, not a pointer, null, and won't (certinaly shouldn't) be used as such later. Why do you feel that would influence a crash?

                      Pl45m4P 1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @JonB said in QByteArray toLong crash:

                        Why do you feel that would influence a crash?

                        val = ba.toLong(...); // returns 0
                        newVal = 42 / val

                        :)

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

                        JonBJ 1 Reply Last reply
                        4
                        • Christian EhrlicherC Christian Ehrlicher

                          @JonB said in QByteArray toLong crash:

                          Why do you feel that would influence a crash?

                          val = ba.toLong(...); // returns 0
                          newVal = 42 / val

                          :)

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

                          @Christian-Ehrlicher
                          Don't be silly ;-) The input stream is presumably some numbers, the fact that one comes out as 0 shouldn't lead it to be treated as a null pointer is all I'm saying :)

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Pl45m4 said in QByteArray toLong crash:

                            Edit: Interesting, that you ( @J-Hilk ) return la and the value is 0. The null could be the reason for the crash, @sonichy is facing. Depends on where and how that long is used later in the code.

                            ? Even if the value is 0 (I don't know whether it is or not), it's a number, zero, not a pointer, null, and won't (certinaly shouldn't) be used as such later. Why do you feel that would influence a crash?

                            Pl45m4P Offline
                            Pl45m4P Offline
                            Pl45m4
                            wrote on last edited by Pl45m4
                            #14

                            @JonB

                            As I've said, I dont know what OP is trying to do with his long later ( sounds wrong... :-D ).
                            If it's somehow important for the value to be higher than 0, this would definitely crash then... Just my thoughts.
                            And I didnt mentioned any pointer :)


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            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