Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. convert from QByteArray to int or char?

convert from QByteArray to int or char?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
9 Posts 3 Posters 899 Views
  • 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.
  • M Offline
    M Offline
    Mohit Tripathi
    wrote on last edited by Mohit Tripathi
    #1

    Hi,

    how can i convert from QByteaaray to int or char? I am getting the error. I wanna generate the CRC with my given query.

    main()
    {
        sendPacket[0]=0x11;
        sendPacket[1]=0x22;
        sendPacket[2]=0x33;
        sendPacket[3]=0x44;
    
    generateCRC(sendPacket,4);
    }
    
    //this function tested in MCU
    //I wanna convert in Qt.
    
    unsigned int generateCRC(unsigned char *data, unsigned char length)
    {
        unsigned int crc = 0xFFFF;
        unsigned char data2;
        int i;
        for ( i = 0; i < length; i++)
        {
            data2 = *data;
            crc = CRC16(crc, data2);
            *data++;
        }
        return crc;
    }
    
    
    J.HilkJ 1 Reply Last reply
    0
    • M Mohit Tripathi

      Hi,

      how can i convert from QByteaaray to int or char? I am getting the error. I wanna generate the CRC with my given query.

      main()
      {
          sendPacket[0]=0x11;
          sendPacket[1]=0x22;
          sendPacket[2]=0x33;
          sendPacket[3]=0x44;
      
      generateCRC(sendPacket,4);
      }
      
      //this function tested in MCU
      //I wanna convert in Qt.
      
      unsigned int generateCRC(unsigned char *data, unsigned char length)
      {
          unsigned int crc = 0xFFFF;
          unsigned char data2;
          int i;
          for ( i = 0; i < length; i++)
          {
              data2 = *data;
              crc = CRC16(crc, data2);
              *data++;
          }
          return crc;
      }
      
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Mohit-Tripathi

      generateCRC(reinterpret_cast<unsigned char*>(sendPacket.data()), sendPacket.size());
      

      this is basic c++, you should know this.


      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
      • M Offline
        M Offline
        Mohit Tripathi
        wrote on last edited by Mohit Tripathi
        #3

        Hi @J-Hilk ,

        I am not much familiar with the C++ and Qt. I am trying to do my stuff and learning these things.
        I have more question:
        I am getting the error in above function.
        "error: undefined reference to `Dialog::generateCRC(unsigned char, unsigned char)'*"
        Can I know, what is this error exactly?

        Thanks

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • M Mohit Tripathi

          Hi @J-Hilk ,

          I am not much familiar with the C++ and Qt. I am trying to do my stuff and learning these things.
          I have more question:
          I am getting the error in above function.
          "error: undefined reference to `Dialog::generateCRC(unsigned char, unsigned char)'*"
          Can I know, what is this error exactly?

          Thanks

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mohit-Tripathi said in convert from QByteArray to int or char?:

          Can I know, what is this error exactly?

          It tells you that Dialog::generateCRC(unsigned char, unsigned char)'* is not defined (does not exist).
          So, do you have such a method in your code?
          Can you please show your current code, else we will guess too much...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • M Offline
            M Offline
            Mohit Tripathi
            wrote on last edited by
            #5

            Hi @jsulm ,

            Please check my first post above. I am trying to do that only.

            Thanks

            jsulmJ 1 Reply Last reply
            0
            • M Mohit Tripathi

              Hi @jsulm ,

              Please check my first post above. I am trying to do that only.

              Thanks

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Mohit-Tripathi OK, so take a look at what you have above:

              unsigned int generateCRC(unsigned char *data, unsigned char length)
              

              generateCRC has two parameters:

              • unsigned char *data
              • unsigned char length

              The error message suggests me that you are passing a char as first parameter and not a pointer to char:
              "error: undefined reference to `Dialog::generateCRC(unsigned char, unsigned char)'*"

              So, can you please show how you are using generateCRC?!

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Mohit Tripathi

                Hi @J-Hilk ,

                I am not much familiar with the C++ and Qt. I am trying to do my stuff and learning these things.
                I have more question:
                I am getting the error in above function.
                "error: undefined reference to `Dialog::generateCRC(unsigned char, unsigned char)'*"
                Can I know, what is this error exactly?

                Thanks

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

                @Mohit-Tripathi

                I am not much familiar with the C++ and Qt. I am trying to do my stuff and learning these things.

                fair enough, but c++ is not an easy language to jump into and self study

                I am getting the error in above function.
                "error: undefined reference to `Dialog::generateCRC(unsigned char, unsigned char)'*"

                you'll need to show us your actual class file *.h and *.cpp as the reinterpret cast will work with the function you provided in the opening post. I tested that, just to make sure.
                There will be warnings, because the size returns an int, that will be implicitly cast to unsigned char but it should compile.


                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
                0
                • M Offline
                  M Offline
                  Mohit Tripathi
                  wrote on last edited by Mohit Tripathi
                  #8

                  Hi @J-Hilk and @jsulm ,

                  I have fixed above issue.

                  But, I am not not getting the expected CRC. My query is 7e03010e60c150a16edd68d108bb2ba3 and the CRC-16 should be 0x0896. You can check the CRC on https://www.lammertbies.nl/comm/info/crc-calculation.
                  But, I am getting the 2198 return value of CRC.
                  Can I know, why I am getting the different CRC here?

                  Thanks

                  J.HilkJ 1 Reply Last reply
                  0
                  • M Mohit Tripathi

                    Hi @J-Hilk and @jsulm ,

                    I have fixed above issue.

                    But, I am not not getting the expected CRC. My query is 7e03010e60c150a16edd68d108bb2ba3 and the CRC-16 should be 0x0896. You can check the CRC on https://www.lammertbies.nl/comm/info/crc-calculation.
                    But, I am getting the 2198 return value of CRC.
                    Can I know, why I am getting the different CRC here?

                    Thanks

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

                    @Mohit-Tripathi
                    (hex)896 == (int)2198


                    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

                    • Login

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