I have a QString StrData = "abcd" and I want get the Ascii value in hex of that string and Vice Versa.
For example from "abcd" to "61 62 63 64" and from "61 62 63 64" to "abcd"
I manage to get the Ascii value in hex but don't know how to get it back
Qstring StrData = "abcd";
Qstring HexStrData;
for (int i = 0; i < StrData.length(); i++) {
HexStrData.append(Qstring::number(StrData.at(i).unicode(), 16));
HexStrData.append(" ");
}
To do the first conversion you can use the following method:
QString StrData = "abcd";
qDebug()<<"before "<< StrData;
QStringList numberString;
for(const auto character: StrData){
numberString << QString::number(character.unicode(), 16);
}
QString HexStrData= numberString.join(" ");
qDebug()<<HexStrData;
For the second case is much simpler as I show below:
QString str = QByteArray::fromHex(HexStrData.remove(" ").toLocal8Bit());
qDebug()<<str;
Output:
before "abcd"
"61 62 63 64"
"abcd"
Example
QString hex("0123456789ABCDEF");
QString strStr("abcd");
QString hexStr;
for (int ii(0); ii < strStr.length(); ii++)
{
hexStr.append(hex.at(strStr.at(ii).toLatin1() >> 4));
hexStr.append(hex.at(strStr.at(ii).toLatin1() & 0x0F));
}
qDebug() << hexStr;
QByteArray oldStr = QByteArray::fromHex(hexStr.toLocal8Bit());
qDebug() << oldStr.data();
Shows:
"61626364"
abcd
Related
I'm trying to encrypt a large file using OpenSSL AES_set_encrypt_key & AES_cbc_encrypt functions. I've written a C++ program to encrypt a file using AES_set_encrypt_key & AES_cbc_encrypt. The contents is got from a large file. I'm allocating memory in heap and calling AES_cbc_encrypt. But I observed only 8 bytes are getting encrypted. But when I allocate memory on stack, the function is working properly. Any help is highly appreciated.
unsigned char enc_out[encsize]; //working
unsigned char *enc_out = new unsigned char[encsize];//not working
`
QByteArray WebMessages::encryption(QString e_text) {
QByteArray ba = e_text.toLatin1();
char *encinput = strdup(ba.constData());
const int UserDataSize = strlen(encinput);
unsigned char *test2 = new unsigned char[UserDataSize];
for (int i = 0; i < UserDataSize; i++) {
test2[i] = encinput[i];
// qDebug() << test2[i];
}
int keylength = 128;
unsigned char aes_key[] = "";
unsigned char iv_enc[] = "";
unsigned char iv_dec[] = "";
const int encsize =
((UserDataSize + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
qDebug() << "$$$$$$$$$$$" << UserDataSize << AES_BLOCK_SIZE << encsize
<< sizeof(unsigned char *); //1481 16 1488 8
unsigned char enc_out[encsize];
// unsigned char *enc_out = new unsigned char[encsize];
AES_KEY enc_key;
AES_set_encrypt_key(aes_key, keylength, &enc_key);
AES_cbc_encrypt(test2, enc_out, UserDataSize, &enc_key, iv_enc,
AES_ENCRYPT);
qDebug() << "enc_out" << enc_out << sizeof(enc_out);
}
`
I want to communicate between two application using fifo (ipc), I have created a fifo by "mkfifo MyPipe" command.
first App:
....
....
fd = open("MyPipe", O_NONBLOCK | O_WRONLY);
QByteArray buf;
QDataStream bdwr(&buf,QIODevice::WriteOnly);
bdwr.setVersion(kDSVersion);
myclassObjWr.lname = "AAAAA";
myclassObjWr.fname = "BBBBB";
bdwr << myclassObjWr.lname << myclassObjWr.fname ;
ssize_t written = write(fd,buf.data() , buf.length());
.....
.....
second App:
....
....
fd = open("MyPipe", O_NONBLOCK | O_RDONLY);
ssize_t nread;
QByteArray rxbuf;
static const QDataStream::Version kDSVersion = QDataStream::Qt_5_12;
QDataStream bdrd(&rxbuf, QIODevice::ReadWrite);
bdrd.setVersion(kDSVersion);
nread = read(fd, rxbuf.data(),200);
if ( nread > 0)
{
bdrd.writeRawData(rxbuf.data(),nread);
bdrd >> myclassObjrd.lname >> myclassObjrd.fname;
qDebug() << "lname: " << myclassObjrd.lname;
}
....
....
After reading the ipc fifo the "rxbuf" is initialized completely but "myclassObjrd.lname" and "myclassObjrd.fname" both are empty and do not initialize.
It was solved by using an intermediate unsigned char buffer to initialize QByteArray in the Second App as follows.
# ...
ssize_t nread;
QByteArray buf;
unsigned char* rxbuf;
static const QDataStream::Version kDSVersion = QDataStream::Qt_5_12;
QDataStream bdrd(&buf, QIODevice::ReadOnly);
bdrd.setVersion(kDSVersion);
rxbuf = (unsigned char*)malloc(200);
nread = read(fd, rxbuf,200);
if ( nread > 0)
{
for (int i = 0 ; i < nread; i++) {
buf[i] = rxbuf[i] ; //init QByteArray
}
bdrd >> myclassObjrd.lname >> myclassObjrd.fname;
qDebug() << "lname: " << myclassObjrd.lname;
}
# ...
My question will be Arduino specific, I wrote a code that turns array of characters (text) into binary string, but the problem is that the binary representation is not 8 bits, its sometimes 7 bits, 6 bits or even 1 bit representation (if you have a value of 1 as decimal). I'm using String constructor String(letter, BIN) to store the binary representation of letter in a string.
I would like to have a 8 bits representation or even a 7 bits representation.
String text = "meet me in university";
String inbits;
byte after;
byte bits[8];
byte x;
char changed_char;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Press anything to begin");
inbits = convertToBits(text);
}
String convertToBits(String plaintext)
{
String total,temp;
total = String(plaintext[0],BIN);
total = String(total + " ");
for (int i=1;i<plaintext.length();i++)
{
temp = String (plaintext[i],BIN);
total = String(total + temp);
total = String(total + " ");
}
Serial.println(total);
return total;
}
If the length of the argument string is less then 8, prepend "0"s until it is 8 bits long.
You could do something similar to the following:
void PrintBinary(const std::string& test)
{
for (int c = 0; c < test.length(); c++)
{
unsigned bits = (unsigned)test[c];
for (int i = 0; i < 8; i++)
{
std::cout << ((bits >> (7 - i)) & 1U);
}
std::cout << " ";
}
}
Modifying the above example to use String and Serial.println instead of std::string and std::cout should be trivial. I don't own an arduino to test with so I couldn't modify your code and test if the above is possible in the environment you work in but I assume it is.
PrintBinary("Hello"); //Output: 01001000 01100101 01101100 01101100 01101111
String(letter, BIN) doesn't zero pad the string. You have to do it yourself.
You need to prepend the 0 character until your binary string is 8 characters long.
String convertToBits(String plaintext)
{
String total, temp;
total = "";
for (int i=0; i<plaintext.length(); i++)
{
temp = String (plaintext[i], BIN);
while (temp.length() < 8)
temp = '0' + temp;
if (i > 0)
total = String(total + " ");
total = String(total + temp);
}
Serial.println(total);
return total;
}
I want to convert byte data that stored in QBytearray into string value. that string value am using it for displaying in ui window..
QByteArray array;
array.append( 0x02 );
array.append( 0xC1);
qDebug()<<( uint )array[0]<<" "<<( uint )array[1];
uint i = 0x00000000;
i |= array[1];
qDebug()<<i;
uint j = 0x00000000 | ( array[0] << 8 );
qDebug()<<j;
i |= j;
bool b = false;
QString str = QString::number( i );
qDebug()<<str;
but the str prints "4294967233"...this code works for some of the bytes like 0x1, 0x45 and for some of other..but this code not working perfectly for all bytes of data into string..please help me with this and write code for this and post it here..thanks
All values equal or bigger than 0x80 interprets in your sample as negative values, so it need cast to unsigned type before bitwise operations.
QByteArray array;
array.append( 0x02 );
array.append( 0xC1);
unsigned int value = 0;
for (int i = 0; i < array.size(); i++)
value = (value << 8) | static_cast<unsigned char>(array[i]);
QString str = QString::number(value);
qDebug() << value << str;
Lets say we have a variable called X and we do some operations on it. now for printing it on the QtextEdit I want to print it like this cout on console:
cout << "The value of X is " << X << endl;
But the setText function only prints out a QString not both "the value of ... " and X.
You can use a QTextStream to write data into a QString similar to cout:
int X = 42;
QString str;
QTextStream out(&str);
out << "The value of X is " << X << endl;
qDebug() << str;
Output:
"The value of X is 42
"
I would solve this in the following way:
QString text = QString("This is my value: %1").arg(x); // x can be either number or string
textEdit->setText(text);
If your "x" is an integer, for example, you can convert that number into a string and concatenate that with the introducing string like that:
QString myText = "This is my value: " + QString::number(x);
If x=5 this will give you this string:
This is my value: 5
You can now assign myText to your QTextEdit with settext.