Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create hex value for QByteArray from int
QtWS25 Last Chance

Create hex value for QByteArray from int

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 6 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manu_NaFu
    wrote on 21 Feb 2022, 19:52 last edited by
    #1

    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.

    J 1 Reply Last reply 22 Feb 2022, 06:25
    0
    • M Manu_NaFu
      22 Feb 2022, 10:58

      @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.

      J Offline
      J Offline
      JonB
      wrote on 22 Feb 2022, 10:59 last edited by JonB
      #19

      @Manu_NaFu
      Which is what we said originally with char(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 either 0..255 or -128..127. Does your int_value always lie in that range? Never a number > 255?

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mpergand
        wrote on 21 Feb 2022, 20:17 last edited by mpergand
        #2

        Try any of this:

        "\\x"
        R"(\x)"  // c++11
        
        M 1 Reply Last reply 22 Feb 2022, 09:53
        4
        • M Manu_NaFu
          21 Feb 2022, 19:52

          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.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 22 Feb 2022, 06:25 last edited by
          #3

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • M mpergand
            21 Feb 2022, 20:17

            Try any of this:

            "\\x"
            R"(\x)"  // c++11
            
            M Offline
            M Offline
            Manu_NaFu
            wrote on 22 Feb 2022, 09:53 last edited by Manu_NaFu
            #4

            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.

            J J 2 Replies Last reply 22 Feb 2022, 09:57
            0
            • M Manu_NaFu
              22 Feb 2022, 09:53

              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.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 22 Feb 2022, 09:57 last edited by
              #5

              @Manu_NaFu Second option requires C++11 as pointed out by @mpergand
              Please show how you used the first option.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Manu_NaFu
                22 Feb 2022, 09:53

                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.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 22 Feb 2022, 09:57 last edited by
                #6

                @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?


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Manu_NaFu
                  wrote on 22 Feb 2022, 10:03 last edited by Manu_NaFu
                  #7

                  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.

                  J KroMignonK 2 Replies Last reply 22 Feb 2022, 10:06
                  0
                  • M Manu_NaFu
                    22 Feb 2022, 10:03

                    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.

                    J Offline
                    J Offline
                    JonB
                    wrote on 22 Feb 2022, 10:06 last edited by JonB
                    #8

                    @Manu_NaFu
                    So you must put your int into the QByteArray. I don't understand what you are saying. What type is aux and what value does it have?? You are saying it is a string whose value is Value if you end up with \xValue???

                    UPDATE
                    OK so you have now shown aux is a QString from a QTextEdit, could you please clarify what its actual value is, and what the actual value in value ends up being? Because so far you have said its value is Value....

                    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 \\....

                    1 Reply Last reply
                    0
                    • M Manu_NaFu
                      22 Feb 2022, 10:03

                      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.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on 22 Feb 2022, 10:08 last edited by KroMignon
                      #9

                      @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.

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Manu_NaFu
                        wrote on 22 Feb 2022, 10:17 last edited by Manu_NaFu
                        #10

                        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)

                        KroMignonK J 2 Replies Last reply 22 Feb 2022, 10:24
                        0
                        • M Manu_NaFu
                          22 Feb 2022, 10:17

                          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)

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on 22 Feb 2022, 10:24 last edited by
                          #11

                          @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 the QByteArray

                          QByteArray value;
                          
                          value.append(char(100));
                          

                          b. Use QDataStream

                          QByteArray value;
                          QDataStream stream(&value, QIODevice::WriteOnly);
                          stream << char(100);
                          

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          M 1 Reply Last reply 22 Feb 2022, 10:28
                          1
                          • M Manu_NaFu
                            22 Feb 2022, 10:17

                            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)

                            J Offline
                            J Offline
                            JonB
                            wrote on 22 Feb 2022, 10:26 last edited by
                            #12

                            @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.....

                            1 Reply Last reply
                            0
                            • KroMignonK KroMignon
                              22 Feb 2022, 10:24

                              @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 the QByteArray

                              QByteArray value;
                              
                              value.append(char(100));
                              

                              b. Use QDataStream

                              QByteArray value;
                              QDataStream stream(&value, QIODevice::WriteOnly);
                              stream << char(100);
                              
                              M Offline
                              M Offline
                              Manu_NaFu
                              wrote on 22 Feb 2022, 10:28 last edited by Manu_NaFu
                              #13

                              @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.

                              J KroMignonK 2 Replies Last reply 22 Feb 2022, 10:41
                              0
                              • M Manu_NaFu
                                22 Feb 2022, 10:28

                                @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.

                                J Offline
                                J Offline
                                JonB
                                wrote on 22 Feb 2022, 10:41 last edited by JonB
                                #14

                                @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?

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  Manu_NaFu
                                  wrote on 22 Feb 2022, 10:46 last edited by Manu_NaFu
                                  #15

                                  @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.

                                  J 1 Reply Last reply 22 Feb 2022, 10:50
                                  0
                                  • M Manu_NaFu
                                    22 Feb 2022, 10:28

                                    @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.

                                    KroMignonK Offline
                                    KroMignonK Offline
                                    KroMignon
                                    wrote on 22 Feb 2022, 10:47 last edited by KroMignon
                                    #16

                                    @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);
                                    }
                                    

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    1 Reply Last reply
                                    1
                                    • M Manu_NaFu
                                      22 Feb 2022, 10:46

                                      @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.

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 22 Feb 2022, 10:50 last edited by JonB
                                      #17

                                      @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....

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        Manu_NaFu
                                        wrote on 22 Feb 2022, 10:58 last edited by Manu_NaFu
                                        #18

                                        @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.

                                        J 1 Reply Last reply 22 Feb 2022, 10:59
                                        0
                                        • M Manu_NaFu
                                          22 Feb 2022, 10:58

                                          @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.

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 22 Feb 2022, 10:59 last edited by JonB
                                          #19

                                          @Manu_NaFu
                                          Which is what we said originally with char(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 either 0..255 or -128..127. Does your int_value always lie in that range? Never a number > 255?

                                          1 Reply Last reply
                                          1

                                          1/19

                                          21 Feb 2022, 19:52

                                          • Login

                                          • Login or register to search.
                                          1 out of 19
                                          • First post
                                            1/19
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved