Unsolved convert from QByteArray to int or char?
-
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; }
-
generateCRC(reinterpret_cast<unsigned char*>(sendPacket.data()), sendPacket.size());
this is basic c++, you should know this.
-
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
-
@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... -
-
@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?!
-
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 thesize
returns an int, that will be implicitly cast to unsigned char but it should compile. -
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
-
@Mohit-Tripathi
(hex)896 == (int)2198