Qstring to unsigned char conversion function
-
wrote on 5 Oct 2019, 13:24 last edited by JKSH 10 May 2019, 14:14
Dear all,
i have this variable:
unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
and using this function to convert it to QString:
QString CharToString(unsigned char *str) { QString result = ""; int lengthOfString = strlen(reinterpret_cast<const char*>(str)); // print string in reverse order QString s; for(int i = 0; i < lengthOfString; i++) { s = QString("%1").arg(str[i], 0, 16); // account for single-digit hex values (always must serialize as two digits) if(s.length() == 1) result.append("0"); result.append(s); } return result; }
now I am trying to figure out SAME function to convert the QString again to unsigned char.
Im very lost and tried many solutions i found online, nothing worked...
Can you pleas ehelp me to make new funciton StringToChar() to convert the Qstring into unsigned char?
Thank you!!
-
Dear all,
i have this variable:
unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
and using this function to convert it to QString:
QString CharToString(unsigned char *str) { QString result = ""; int lengthOfString = strlen(reinterpret_cast<const char*>(str)); // print string in reverse order QString s; for(int i = 0; i < lengthOfString; i++) { s = QString("%1").arg(str[i], 0, 16); // account for single-digit hex values (always must serialize as two digits) if(s.length() == 1) result.append("0"); result.append(s); } return result; }
now I am trying to figure out SAME function to convert the QString again to unsigned char.
Im very lost and tried many solutions i found online, nothing worked...
Can you pleas ehelp me to make new funciton StringToChar() to convert the Qstring into unsigned char?
Thank you!!
Lifetime Qt Championwrote on 5 Oct 2019, 13:54 last edited by aha_1980 10 May 2019, 14:49Hi @shokarta,
if I get it right, all you want to do is to convert the array
plain[]
to a hexadecimal string.You can easily do this with:
QString s = QByteArray(reinterpret_cast<const char*>(plain)).toHex()
instead your complicated function.The other way round can be done with
QByteArray::fromHex()
:const QString s ="0123456789ABCDEF"; const QByteArray ba = QByteArray::fromHex(s.toLatin1()); const unsigned char *plain = reinterpret_cast<const unsigned char *>(ba.constData());
Regards
-
Hi @shokarta,
if I get it right, all you want to do is to convert the array
plain[]
to a hexadecimal string.You can easily do this with:
QString s = QByteArray(reinterpret_cast<const char*>(plain)).toHex()
instead your complicated function.The other way round can be done with
QByteArray::fromHex()
:const QString s ="0123456789ABCDEF"; const QByteArray ba = QByteArray::fromHex(s.toLatin1()); const unsigned char *plain = reinterpret_cast<const unsigned char *>(ba.constData());
Regards
wrote on 5 Oct 2019, 14:22 last edited by@aha_1980
Hello,many thanks for helping, so I have this then:
unsigned char StringToChar(QString *str) { const QByteArray ba = QByteArray::fromHex(str->toLatin1()); const unsigned char result[] = reinterpret_cast<const unsigned char*>(ba.constData()); return result; }
which brings error on line const unsigned char:
error: array initializer must be an initializer list or string literal -
@aha_1980
Hello,many thanks for helping, so I have this then:
unsigned char StringToChar(QString *str) { const QByteArray ba = QByteArray::fromHex(str->toLatin1()); const unsigned char result[] = reinterpret_cast<const unsigned char*>(ba.constData()); return result; }
which brings error on line const unsigned char:
error: array initializer must be an initializer list or string literalSorry, I missed a star, I meant
const unsigned char *plain = reinterpret_cast<const unsigned char *>(ba.constData());
Fixed my example.
unsigned char StringToChar(QString *str)
Why do you pass str by pointer here?
const unsigned char result = reinterpret_cast<const unsigned char>(ba.constData());
return result;Note that you cannot do that! the local variable is destroyed when you leave the function. You can only copy the result into a buffer provided from outside.
Regards
-
wrote on 5 Oct 2019, 15:09 last edited by shokarta 10 May 2019, 15:10
the pointer was placed because QT gives me error and suggest to do that...
so basicaly when u do this:
unsigned char StringToChar(QString *str) { const QByteArray ba = QByteArray::fromHex(str.toLatin1()); const unsigned char *result = reinterpret_cast<const unsigned char *>(ba.constData()); return result; }
i have error on line const QbyteArray ba:
main.cpp:23:50: error: member reference type 'QString *' is a pointer; did you mean to use '->'?
and another error on line return result:
main.cpp:25:12: error: cannot initialize return object of type 'unsigned char' with an lvalue of type 'const unsigned char *'
-
the pointer was placed because QT gives me error and suggest to do that...
so basicaly when u do this:
unsigned char StringToChar(QString *str) { const QByteArray ba = QByteArray::fromHex(str.toLatin1()); const unsigned char *result = reinterpret_cast<const unsigned char *>(ba.constData()); return result; }
i have error on line const QbyteArray ba:
main.cpp:23:50: error: member reference type 'QString *' is a pointer; did you mean to use '->'?
and another error on line return result:
main.cpp:25:12: error: cannot initialize return object of type 'unsigned char' with an lvalue of type 'const unsigned char *'
the pointer was placed because QT gives me error and suggest to do that...
That is no reason to do it like this. Fix it please. Either use
QString str
or (better)const QString &str
as parameter.main.cpp:25:12: error: cannot initialize return object of type 'unsigned char' with an lvalue of type 'const unsigned char *'
That is because the return value does not match the functions signature.
But again: you cannot return a pointer to a local variable! You can use it within the function, but not outside.
Better explain first why you need
const unsigned char *
at all, then we can find a solution.Regards
-
wrote on 5 Oct 2019, 17:57 last edited by
basicaly i hae dowloaded and used huge function to AES encode and decode string.
the function is loaded by unsigned char:const unsigned int BLOCK_BYTES_LENGTH = 16 * sizeof(unsigned char); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; unsigned char iv[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; unsigned int len; AES aes(256); unsigned char *out = aes.EncryptCBC(plain, BLOCK_BYTES_LENGTH, key, iv, len);
as well as it outputs the encrypted string in unsigned char
Hoewer because its a huge function which i understand just on basic logical concept i prefer not to use modify the function to insert string instead of char, as well as output, so i would prefer to convert it (i will be using this on many places, therefore the function is desired)
so then i could only use:
unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);
-
basicaly i hae dowloaded and used huge function to AES encode and decode string.
the function is loaded by unsigned char:const unsigned int BLOCK_BYTES_LENGTH = 16 * sizeof(unsigned char); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; unsigned char iv[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; unsigned int len; AES aes(256); unsigned char *out = aes.EncryptCBC(plain, BLOCK_BYTES_LENGTH, key, iv, len);
as well as it outputs the encrypted string in unsigned char
Hoewer because its a huge function which i understand just on basic logical concept i prefer not to use modify the function to insert string instead of char, as well as output, so i would prefer to convert it (i will be using this on many places, therefore the function is desired)
so then i could only use:
unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);
@shokarta said in Qstring to unsigned char conversion function:
aes.EncryptCBC
can you please show the function signature of that function?
-
wrote on 5 Oct 2019, 18:19 last edited by
its downloaded and used from here:
https://github.com/SergeyBel/AESi have done no futher changes on these files
-
its downloaded and used from here:
https://github.com/SergeyBel/AESi have done no futher changes on these files
So the signature is:
unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
None of these parameters is
const
, so that means the function can modifyin
,key
, andiv
(not sure if it does modify these parameters, but it could).That means, you have to create local buffers for these parameters, and copy the data into the buffers.
Especially, this
unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);
is not possible!
Regards
-
So the signature is:
unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
None of these parameters is
const
, so that means the function can modifyin
,key
, andiv
(not sure if it does modify these parameters, but it could).That means, you have to create local buffers for these parameters, and copy the data into the buffers.
Especially, this
unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);
is not possible!
Regards
wrote on 5 Oct 2019, 19:08 last edited by@aha_1980
Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
For skilled one it could be matrer of several minutes only :/.
I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
Any chance you and i can have any deal on that?
Only thus one aes-cbc-256 function would be needed, other methods not at all... -
@aha_1980
Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
For skilled one it could be matrer of several minutes only :/.
I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
Any chance you and i can have any deal on that?
Only thus one aes-cbc-256 function would be needed, other methods not at all... -
@aha_1980
Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
For skilled one it could be matrer of several minutes only :/.
I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
Any chance you and i can have any deal on that?
Only thus one aes-cbc-256 function would be needed, other methods not at all...@shokarta said in Qstring to unsigned char conversion function:
Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
Why? This library does not use Qt. Simply use these functions the way they are supposed to be used.
Also, https://github.com/SergeyBel/AES/blob/master/README.md shows how to use the functions. -
@aha_1980
Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
For skilled one it could be matrer of several minutes only :/.
I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
Any chance you and i can have any deal on that?
Only thus one aes-cbc-256 function would be needed, other methods not at all...wrote on 7 Oct 2019, 05:51 last edited by@shokarta
If you want a changed signature in a library, especially if it's just one function, the usual way is you write the desired wrapper function and go via that. Is there any reason you cannot do this for your usage? Over the years I have found it's often a good idea to write wrapper functions/classes for more or less every external library one uses, for all sorts of reasons. Heck, I don't even call the Qt classes/methods directly, I tend to provide wrappers for all of them!
1/14