Create hex value for QByteArray from int
-
wrote on 21 Feb 2022, 19:52 last edited by
Hello,
I have an int value which I want to send through BLE (bluetooth low energy), so I need to create a string like "\xValue".
I am able to convert the int value to hex and assign it to QByteArray, but I'm not able to attach "\x" string , which is needed for BLE.
Thanks in advance.
-
@JonB I understand, then I would need another way to transform int value to a real byte.
With char(value) as @KroMignon pointed, it works sending an int value just as:
QByteArray value; value.append(char(int_value));
Sorry, I meant to say that sending a fixed value was wrong, but I didn't know the way to convert int to byte was using char(), but makes sense since char is a byte.
My value will be a percentage between 0 and 100 so it will work fine from 0 to 255, but also thanks for the tip.
Thanks for all the help and sorry for the missunderstandings.
wrote on 22 Feb 2022, 10:59 last edited by JonB@Manu_NaFu
Which is what we said originally withchar(100)
and you said that was wrong/did not work....to a real byte in hex.
Please note, there is no such thing as a byte "in hex"! A byte is a byte! there is no "in hex", "in decimal" or "in binary"! :) They only mean anything if you transform a value from/to a string representation of the value.
Forget everything about any
\x
! You just need to send your desired bytes as-is.One comment about
value.append(char(int_value));
. Note this will only send one byte, in range either0..255
or-128..127
. Does yourint_value
always lie in that range? Never a number > 255? -
wrote on 21 Feb 2022, 20:17 last edited by mpergand
Try any of this:
"\\x" R"(\x)" // c++11
-
Hello,
I have an int value which I want to send through BLE (bluetooth low energy), so I need to create a string like "\xValue".
I am able to convert the int value to hex and assign it to QByteArray, but I'm not able to attach "\x" string , which is needed for BLE.
Thanks in advance.
@Manu_NaFu said in Create hex value for QByteArray from int:
but I'm not able to attach "\x" string
array.append("\\x"); array.append(YOUR_HEY_HERE);
You need to escape \ if it needs to be part of a string.
-
Hi @mpergand thanks for your response. The first option appends both backslashes "\\xValue", and second option I cannot use only one backslash, I get this error: error: \x used with no following hex digits.
@Manu_NaFu Second option requires C++11 as pointed out by @mpergand
Please show how you used the first option. -
Hi @mpergand thanks for your response. The first option appends both backslashes "\\xValue", and second option I cannot use only one backslash, I get this error: error: \x used with no following hex digits.
@Manu_NaFu are you sure, that you're actually sending hex encoded strings via BTLE?
Or are you actually sending an int in bytes via a BTLE datagram?
-
wrote on 22 Feb 2022, 10:03 last edited by Manu_NaFu
Hi @jsulm @J-Hilk , I need to send "\xValue" in a QbyteArray for BLE. When I send something like "\x64", I get 100 in my bluetooth device (using arduino BLE), which is the expected value (0x64 = 100).
But that only works if I specifically create the string directly as is: "\x64" and then send it, but what I want to do is create this string from an int, so I need to add "\x" to the int value converted to hex. I can do the second step but not the first one, this is the code:
QString aux = ui->textEdit->toPlainText(); QByteArray value; value.append("\\x"); value.append(aux.toUtf8());
But then I get the string I mentioned, "\\xValue".
I also have: CONFIG += c++11
Thanks for the help.
-
Hi @jsulm @J-Hilk , I need to send "\xValue" in a QbyteArray for BLE. When I send something like "\x64", I get 100 in my bluetooth device (using arduino BLE), which is the expected value (0x64 = 100).
But that only works if I specifically create the string directly as is: "\x64" and then send it, but what I want to do is create this string from an int, so I need to add "\x" to the int value converted to hex. I can do the second step but not the first one, this is the code:
QString aux = ui->textEdit->toPlainText(); QByteArray value; value.append("\\x"); value.append(aux.toUtf8());
But then I get the string I mentioned, "\\xValue".
I also have: CONFIG += c++11
Thanks for the help.
wrote on 22 Feb 2022, 10:06 last edited by JonB@Manu_NaFu
So you must put yourint
into theQByteArray
. I don't understand what you are saying. What type isaux
and what value does it have?? You are saying it is a string whose value isValue
if you end up with\xValue
???UPDATE
OK so you have now shownaux
is aQString
from aQTextEdit
, could you please clarify what its actual value is, and what the actual value invalue
ends up being? Because so far you have said its value isValue
....And btw if you are worried the final string contains
\\x...
instead of\x...
it does not. The debugger shows strings with\
in them as\\
.... -
Hi @jsulm @J-Hilk , I need to send "\xValue" in a QbyteArray for BLE. When I send something like "\x64", I get 100 in my bluetooth device (using arduino BLE), which is the expected value (0x64 = 100).
But that only works if I specifically create the string directly as is: "\x64" and then send it, but what I want to do is create this string from an int, so I need to add "\x" to the int value converted to hex. I can do the second step but not the first one, this is the code:
QString aux = ui->textEdit->toPlainText(); QByteArray value; value.append("\\x"); value.append(aux.toUtf8());
But then I get the string I mentioned, "\\xValue".
I also have: CONFIG += c++11
Thanks for the help.
wrote on 22 Feb 2022, 10:08 last edited by KroMignon@Manu_NaFu said in Create hex value for QByteArray from int:
But that only works if I specifically create the string directly as is: "\x64" and then send it, but what I want to do is create this string from an int, so I need to add "\x" to the int value converted to hex. I can do the second step but not the first one, this is the code:
QByteArray value;
value.append("\x");
value.append(aux.toUtf8());But then I get the string I mentioned, "\xValue".
This don't make sense to me.
I guess you have looked at variable content in a debugger and have seen "\x64". Which means that iit is a string which contains a byte with value 0x64. -
wrote on 22 Feb 2022, 10:17 last edited by Manu_NaFu
Hi @JonB @KroMignon sorry I can only send one response every 10 minutes.
@KroMignon my issue is that if I create the QByteArray as:
value.append("\x64");
It will send the correct value and will be read by my bluetooth device as 100 (0x64 = 100), but I cannot just create a fixed value for the QbyteArray, I need to get it from an int that I have in my code.
@JonB The string from the QTextEdit is just an aux, it will eventually be an int, but It's just easier for me at this point to get the value from QTextEdit. The thing is that the problem is not getting an int or a string to hex, but to append "\x" at the start of the value of QByteArray. I see the string contains double backlash when print it, and also when my bluetooth device gets the value, it's different if I send "\x64" (value=100, expected) and "\\x64" (value=120, not expected)
-
Hi @JonB @KroMignon sorry I can only send one response every 10 minutes.
@KroMignon my issue is that if I create the QByteArray as:
value.append("\x64");
It will send the correct value and will be read by my bluetooth device as 100 (0x64 = 100), but I cannot just create a fixed value for the QbyteArray, I need to get it from an int that I have in my code.
@JonB The string from the QTextEdit is just an aux, it will eventually be an int, but It's just easier for me at this point to get the value from QTextEdit. The thing is that the problem is not getting an int or a string to hex, but to append "\x" at the start of the value of QByteArray. I see the string contains double backlash when print it, and also when my bluetooth device gets the value, it's different if I send "\x64" (value=100, expected) and "\\x64" (value=120, not expected)
wrote on 22 Feb 2022, 10:24 last edited by@Manu_NaFu said in Create hex value for QByteArray from int:
my issue is that if I create the QByteArray as:
value.append("\x64");It will send the correct value and will be read by my bluetooth device as 100 (0x64 = 100), but I cannot just create a fixed value for the QbyteArray, I need to get it from an int that I have in my code.
This is not really difficult to do. There are many ways to proceed:
a. simply add the byte in theQByteArray
QByteArray value; value.append(char(100));
b. Use
QDataStream
QByteArray value; QDataStream stream(&value, QIODevice::WriteOnly); stream << char(100);
-
Hi @JonB @KroMignon sorry I can only send one response every 10 minutes.
@KroMignon my issue is that if I create the QByteArray as:
value.append("\x64");
It will send the correct value and will be read by my bluetooth device as 100 (0x64 = 100), but I cannot just create a fixed value for the QbyteArray, I need to get it from an int that I have in my code.
@JonB The string from the QTextEdit is just an aux, it will eventually be an int, but It's just easier for me at this point to get the value from QTextEdit. The thing is that the problem is not getting an int or a string to hex, but to append "\x" at the start of the value of QByteArray. I see the string contains double backlash when print it, and also when my bluetooth device gets the value, it's different if I send "\x64" (value=100, expected) and "\\x64" (value=120, not expected)
wrote on 22 Feb 2022, 10:26 last edited by@Manu_NaFu
You are being quite unclear about what you want to send, and you are misunderstanding the "presentation" of bytes in the debugger versus what bytes are actually sent/to be sent.If @KroMignon is right and e.g.
value.append(char(100));
works and is what you need to send, then you should never have tried to send a literal\x...
anything..... -
@Manu_NaFu said in Create hex value for QByteArray from int:
my issue is that if I create the QByteArray as:
value.append("\x64");It will send the correct value and will be read by my bluetooth device as 100 (0x64 = 100), but I cannot just create a fixed value for the QbyteArray, I need to get it from an int that I have in my code.
This is not really difficult to do. There are many ways to proceed:
a. simply add the byte in theQByteArray
QByteArray value; value.append(char(100));
b. Use
QDataStream
QByteArray value; QDataStream stream(&value, QIODevice::WriteOnly); stream << char(100);
wrote on 22 Feb 2022, 10:28 last edited by Manu_NaFu@KroMignon But that's not what I need, I need to get this 100 from an int variable, which will not always be 100, so it's not a fixed value.
if it was a fixed value I could do as I told you before, which works:
QByteArray value; value.append("\x64");
@JonB To be clear, I need to send an int variable through a QByteArray, but for BLE to understand the value, it needs to have "\x" at the start of the value, and then the value in hex.
I can get the hex value from the int, but I can't think of a way to add the "\x" to the start. As I told in previous response, When I add "\\x" it will send a different value.
-
@KroMignon But that's not what I need, I need to get this 100 from an int variable, which will not always be 100, so it's not a fixed value.
if it was a fixed value I could do as I told you before, which works:
QByteArray value; value.append("\x64");
@JonB To be clear, I need to send an int variable through a QByteArray, but for BLE to understand the value, it needs to have "\x" at the start of the value, and then the value in hex.
I can get the hex value from the int, but I can't think of a way to add the "\x" to the start. As I told in previous response, When I add "\\x" it will send a different value.
wrote on 22 Feb 2022, 10:41 last edited by JonB@Manu_NaFu said in Create hex value for QByteArray from int:
it needs to have "\x" at the start of the value, and then the value in hex.
I give up. IF you are saying that
value.append("\x64");
, literally as written there with a single\
in code, does work then it is not sending a literal\x
string ..... Assuming you know C++, you tell me what the source code string"\x64"
represents?Forget your "variable value", that is not an issue, we can get that in when we know what you really need to send.
It might be best if you told us the actual bytes you need to send (not a string) --- how many bytes? (1,2,3,4,5??) with what byte values in the bytes?
-
wrote on 22 Feb 2022, 10:46 last edited by Manu_NaFu
@JonB Okay so just to be as clear as I can, I have a variable, imagine it has a value of 100, I would need to send this variable, but for example if I do:
value.setNum(int_variable, 16)
This will have the hex value stored in a QByteArray (needed to be sent through BLE), which will be 64, but will lack the "\x" needed for BLE to understand it's a hex value.
But if instead I do,
value.append("\x64");
It will be understood by BLE and I get the correct value. The downside to this is that I need an actual variable, not a fixed value.
@KroMignon Using char(value) actually worked.
-
@KroMignon But that's not what I need, I need to get this 100 from an int variable, which will not always be 100, so it's not a fixed value.
if it was a fixed value I could do as I told you before, which works:
QByteArray value; value.append("\x64");
@JonB To be clear, I need to send an int variable through a QByteArray, but for BLE to understand the value, it needs to have "\x" at the start of the value, and then the value in hex.
I can get the hex value from the int, but I can't think of a way to add the "\x" to the start. As I told in previous response, When I add "\\x" it will send a different value.
wrote on 22 Feb 2022, 10:47 last edited by KroMignon@Manu_NaFu said in Create hex value for QByteArray from int:
But that's not what I need, I need to get this 100 from an int variable, which will not always be 100, so it's not a fixed value.
if it was a fixed value I could do as I told you before, which works:
QByteArray value;
value.append("\x64");Are you understanding what you are doing?
What are your programming skills?
It is so complicated to understand variable/constant?a. working with constant:
QByteArray value; value.append(char(100)); sendToDevice(value);
b. working with a variable:
for(int value = 12; value < 99; value++) { QByteArray b; b.append(char(value )); sendToDevice(b); }
-
@JonB Okay so just to be as clear as I can, I have a variable, imagine it has a value of 100, I would need to send this variable, but for example if I do:
value.setNum(int_variable, 16)
This will have the hex value stored in a QByteArray (needed to be sent through BLE), which will be 64, but will lack the "\x" needed for BLE to understand it's a hex value.
But if instead I do,
value.append("\x64");
It will be understood by BLE and I get the correct value. The downside to this is that I need an actual variable, not a fixed value.
@KroMignon Using char(value) actually worked.
wrote on 22 Feb 2022, 10:50 last edited by JonB@Manu_NaFu said in Create hex value for QByteArray from int:
but will lack the "\x" needed for BLE to understand it's a hex value
value.append("\x64");
You are not understanding the difference between real byte values and what a debugger shows you when you print what is there.
Please do the following:
QString x("\x64"); qDebug() << x.length();
and tell me what value it shows you?
It prints
1
, doesn't it? So there cannot be any "literal\x
" in the resulting string/byte array, can there....?! And yet you say sending that does work? So that sends a single byte, with no\x
.... -
wrote on 22 Feb 2022, 10:58 last edited by Manu_NaFu
@JonB I understand, then I would need another way to transform int value to a real byte.
With char(value) as @KroMignon pointed, it works sending an int value just as:
QByteArray value; value.append(char(int_value));
Sorry, I meant to say that sending a fixed value was wrong, but I didn't know the way to convert int to byte was using char(), but makes sense since char is a byte.
My value will be a percentage between 0 and 100 so it will work fine from 0 to 255, but also thanks for the tip.
Thanks for all the help and sorry for the missunderstandings.
-
@JonB I understand, then I would need another way to transform int value to a real byte.
With char(value) as @KroMignon pointed, it works sending an int value just as:
QByteArray value; value.append(char(int_value));
Sorry, I meant to say that sending a fixed value was wrong, but I didn't know the way to convert int to byte was using char(), but makes sense since char is a byte.
My value will be a percentage between 0 and 100 so it will work fine from 0 to 255, but also thanks for the tip.
Thanks for all the help and sorry for the missunderstandings.
wrote on 22 Feb 2022, 10:59 last edited by JonB@Manu_NaFu
Which is what we said originally withchar(100)
and you said that was wrong/did not work....to a real byte in hex.
Please note, there is no such thing as a byte "in hex"! A byte is a byte! there is no "in hex", "in decimal" or "in binary"! :) They only mean anything if you transform a value from/to a string representation of the value.
Forget everything about any
\x
! You just need to send your desired bytes as-is.One comment about
value.append(char(int_value));
. Note this will only send one byte, in range either0..255
or-128..127
. Does yourint_value
always lie in that range? Never a number > 255?
1/19