Data Format issue
-
@vidisha
You don't need to convert numbers to strings and vice versa.
Use shift function and work only with numbers:int Data = (3 << 4) | (i1 >> 2);
3 is decimal value for 0011. Result from (3 << 4) is 00110000 binary or 48 decimal.
If i1 = 45 = 101101, then (i1 >> 2) will give you 1011 = 11.
Final result is 00111011 = 59.
Then you can convert it to QString and choose format.
For example:QString res = QString("W %1 1A"").arg(Data, 0, 16).toUpper();
-
@vidisha
You don't need to convert numbers to strings and vice versa.
Use shift function and work only with numbers:int Data = (3 << 4) | (i1 >> 2);
3 is decimal value for 0011. Result from (3 << 4) is 00110000 binary or 48 decimal.
If i1 = 45 = 101101, then (i1 >> 2) will give you 1011 = 11.
Final result is 00111011 = 59.
Then you can convert it to QString and choose format.
For example:QString res = QString("W %1 1A"").arg(Data, 0, 16).toUpper();
-
@Stoyan
One more thing is i have to write 2nd Byte which is 2 LSB's of i1 & value from ( 31.5 to 0).Im poor in Bit masking,
How to get only 2 LSB's of i1 i.e 45(101101) -
@Stoyan
Ya got it,but i have to OR it with value chosen from ( 31.5 to 0)
which has floating point numbers too..int i1 = ui->comboBox_PS1 ->currentText().toFloat();
float AT1 = ui-> comboBox_AT1 ->currentText().toFloat();
int Byt2 = (i1 & 3 );
int BYT3 = Byt2 << 6;
int BYT4 = (BYT3 | (AT1));it gives me error
error: C2297: '|' : illegal, right operand has type 'float'so what's the correct way to convert floating numbers to add with decimal number
-
@Stoyan
Ya got it,but i have to OR it with value chosen from ( 31.5 to 0)
which has floating point numbers too..int i1 = ui->comboBox_PS1 ->currentText().toFloat();
float AT1 = ui-> comboBox_AT1 ->currentText().toFloat();
int Byt2 = (i1 & 3 );
int BYT3 = Byt2 << 6;
int BYT4 = (BYT3 | (AT1));it gives me error
error: C2297: '|' : illegal, right operand has type 'float'so what's the correct way to convert floating numbers to add with decimal number
@vidisha
You can't use bit operations on float or double, because they have completely different inner representations. But if a float number have one digit after the point you can multiply it by 10 and then convert result to integer. With this number you can use bit operations, but result may be different of what you expect.
I don't understand in this case what you want to achieve. Can you give me expected result for a sample input values? -
@vidisha
You can't use bit operations on float or double, because they have completely different inner representations. But if a float number have one digit after the point you can multiply it by 10 and then convert result to integer. With this number you can use bit operations, but result may be different of what you expect.
I don't understand in this case what you want to achieve. Can you give me expected result for a sample input values?@Stoyan
Hi,
I'm doing typecasting I.e converting to char and performing logical operations on it.
This is solved for me.
Tq so much.😊
One more thing I want to know is
I get 2 bytes incoming over serial port.
So, I need to compare each bit with a reference bit.
So how can I access each bit of a byte? -
@Stoyan
Hi,
I'm doing typecasting I.e converting to char and performing logical operations on it.
This is solved for me.
Tq so much.😊
One more thing I want to know is
I get 2 bytes incoming over serial port.
So, I need to compare each bit with a reference bit.
So how can I access each bit of a byte? -
@vidisha
Comparison with one bit:int bit0 = 1; // BIT 0 - 00000001 int bit1 = 2; // BIT 1 - 00000010 ... int bit7 = 128; // BIT 7 - 10000000 ... if (num & bit0) return true; // BIT 0 in num is 1 else return false;
@Stoyan
Hi ,
Actually i send 2 bytes(commands) & in response i get 2 bytes.
I want to check whether received each bit is set or not.
I dont know what each bit should be, but i want to see whether it is 1 or 0.
so for reference i defined -> unsigned short tocheck = 9203( 00100011 11110011)
& i compare each received bit with reference bits.unsigned short tocheck = 9203;
for(int bitnum = 0; bitnum <= 16; bitnum++){
bool biton = (tocheck >> bitnum) & 1;
qDebug() << "bit" << bitnum << "->" << biton;}
Is it correct the way i'm doing?
-
@Stoyan
Hi ,
Actually i send 2 bytes(commands) & in response i get 2 bytes.
I want to check whether received each bit is set or not.
I dont know what each bit should be, but i want to see whether it is 1 or 0.
so for reference i defined -> unsigned short tocheck = 9203( 00100011 11110011)
& i compare each received bit with reference bits.unsigned short tocheck = 9203;
for(int bitnum = 0; bitnum <= 16; bitnum++){
bool biton = (tocheck >> bitnum) & 1;
qDebug() << "bit" << bitnum << "->" << biton;}
Is it correct the way i'm doing?
-
@vidisha
Comparison with one bit:int bit0 = 1; // BIT 0 - 00000001 int bit1 = 2; // BIT 1 - 00000010 ... int bit7 = 128; // BIT 7 - 10000000 ... if (num & bit0) return true; // BIT 0 in num is 1 else return false;
@Stoyan
Hi,
It;s quite not related to above post but i need to solve it urgently so,
I receive 10 Bytes over serial port in hex form,
4F C0 6F C0....AF C0
My incoming data is in m_readData.
QByteArray buf ;
buf += serial_1->readAll();
m_readData.append(buf);
I have to perform bitwise operations on the received data, so i need to convert it into int or binary form,
so , how can i convert incoming hex data to some other form??Regards
Vidisha -
@Stoyan
Hi,
It;s quite not related to above post but i need to solve it urgently so,
I receive 10 Bytes over serial port in hex form,
4F C0 6F C0....AF C0
My incoming data is in m_readData.
QByteArray buf ;
buf += serial_1->readAll();
m_readData.append(buf);
I have to perform bitwise operations on the received data, so i need to convert it into int or binary form,
so , how can i convert incoming hex data to some other form??Regards
Vidisha@vidisha Well, you could read documentation: http://doc.qt.io/qt-5/qbytearray.html#fromHex