Get imei from QByteArray in hex









up vote
0
down vote

favorite












I am developing a desktop application which receives frames in bytes format from a server. I have the imei of the device sending those frames inside the frames received.



I have tried to add to a long long variable with the corresponding left shift to each byte (56, 48, 40..., 0) but number is longer than 40 bits so it does not work.



I just want it to put on a text box so in QString format would be enough but I am not confident at all with C++ and I don't see how I can get it.



For example, this imei: 352353222952355 would be received in a frame like this: [0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3] which I have in my QByteArray. Each time I try to print that variable with qDebug(), I get the hex representation ("x00x...."). What I want to get is the number as I put on the first line in this paragraph.



How can I convert to its decimal representation in a QString?



EDIT:
I want to add some information related to the test I am running with the help of @Peter



This is what I get if I print the QByteArray:



qDebug() << imei;
"x00x01@vx98[xD7xAF"


In case I pass that imei (QByteArray) to your function, I get:
QString imeiStr;
imeiStr = ConvertToQString(imei);



qDebug() << imeiStr;
"u0000u0001@v?[u05EF"


If I debug the application and show both variables (imei and imeiStr) I see some differences:



enter image description here



imeiStr is shorter and also, from position 4, differs from the original.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I am developing a desktop application which receives frames in bytes format from a server. I have the imei of the device sending those frames inside the frames received.



    I have tried to add to a long long variable with the corresponding left shift to each byte (56, 48, 40..., 0) but number is longer than 40 bits so it does not work.



    I just want it to put on a text box so in QString format would be enough but I am not confident at all with C++ and I don't see how I can get it.



    For example, this imei: 352353222952355 would be received in a frame like this: [0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3] which I have in my QByteArray. Each time I try to print that variable with qDebug(), I get the hex representation ("x00x...."). What I want to get is the number as I put on the first line in this paragraph.



    How can I convert to its decimal representation in a QString?



    EDIT:
    I want to add some information related to the test I am running with the help of @Peter



    This is what I get if I print the QByteArray:



    qDebug() << imei;
    "x00x01@vx98[xD7xAF"


    In case I pass that imei (QByteArray) to your function, I get:
    QString imeiStr;
    imeiStr = ConvertToQString(imei);



    qDebug() << imeiStr;
    "u0000u0001@v?[u05EF"


    If I debug the application and show both variables (imei and imeiStr) I see some differences:



    enter image description here



    imeiStr is shorter and also, from position 4, differs from the original.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am developing a desktop application which receives frames in bytes format from a server. I have the imei of the device sending those frames inside the frames received.



      I have tried to add to a long long variable with the corresponding left shift to each byte (56, 48, 40..., 0) but number is longer than 40 bits so it does not work.



      I just want it to put on a text box so in QString format would be enough but I am not confident at all with C++ and I don't see how I can get it.



      For example, this imei: 352353222952355 would be received in a frame like this: [0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3] which I have in my QByteArray. Each time I try to print that variable with qDebug(), I get the hex representation ("x00x...."). What I want to get is the number as I put on the first line in this paragraph.



      How can I convert to its decimal representation in a QString?



      EDIT:
      I want to add some information related to the test I am running with the help of @Peter



      This is what I get if I print the QByteArray:



      qDebug() << imei;
      "x00x01@vx98[xD7xAF"


      In case I pass that imei (QByteArray) to your function, I get:
      QString imeiStr;
      imeiStr = ConvertToQString(imei);



      qDebug() << imeiStr;
      "u0000u0001@v?[u05EF"


      If I debug the application and show both variables (imei and imeiStr) I see some differences:



      enter image description here



      imeiStr is shorter and also, from position 4, differs from the original.










      share|improve this question















      I am developing a desktop application which receives frames in bytes format from a server. I have the imei of the device sending those frames inside the frames received.



      I have tried to add to a long long variable with the corresponding left shift to each byte (56, 48, 40..., 0) but number is longer than 40 bits so it does not work.



      I just want it to put on a text box so in QString format would be enough but I am not confident at all with C++ and I don't see how I can get it.



      For example, this imei: 352353222952355 would be received in a frame like this: [0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3] which I have in my QByteArray. Each time I try to print that variable with qDebug(), I get the hex representation ("x00x...."). What I want to get is the number as I put on the first line in this paragraph.



      How can I convert to its decimal representation in a QString?



      EDIT:
      I want to add some information related to the test I am running with the help of @Peter



      This is what I get if I print the QByteArray:



      qDebug() << imei;
      "x00x01@vx98[xD7xAF"


      In case I pass that imei (QByteArray) to your function, I get:
      QString imeiStr;
      imeiStr = ConvertToQString(imei);



      qDebug() << imeiStr;
      "u0000u0001@v?[u05EF"


      If I debug the application and show both variables (imei and imeiStr) I see some differences:



      enter image description here



      imeiStr is shorter and also, from position 4, differs from the original.







      c++ qt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 20:27

























      asked Nov 10 at 17:03









      user1298272

      182424




      182424






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          quint8 source[8] = 0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3;
          QByteArray imei;
          for(int i = 0; i < 8; ++i)
          imei.append(source[i]);


          qlonglong val = 0;
          for(int i = 0; i < 8; ++i)
          val *= 256;
          val += (quint8)imei.at(i);

          qDebug() << QString::number(val);





          share|improve this answer




















          • That did the trick. Thank you so much! I was trying to shift the number instead of multiplying.
            – user1298272
            Nov 11 at 8:00










          • Shifting for long long is not available, I got the same problem with you.
            – 张铁男
            Nov 11 at 9:25

















          up vote
          1
          down vote













          You can use stringstreams:



          #include <sstream>

          QString ConvertToQString(const QByteArray& array)

          std::stringstream ss;
          for (int i = 0; i < array.size(); ++i)
          ss << array[i];

          return QString::fromStdString(ss.str());






          share|improve this answer






















          • Thanks. I get an error at the return. "no matching conversion for functional-style cast form..." I guess QString does not have that converter. I will check
            – user1298272
            Nov 10 at 17:22










          • Yes, you're right. I've modified the code. Please check this out.
            – Peter
            Nov 10 at 18:47










          • I just added some information about your function. Please, check it.
            – user1298272
            Nov 10 at 20:28










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241306%2fget-imei-from-qbytearray-in-hex%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          quint8 source[8] = 0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3;
          QByteArray imei;
          for(int i = 0; i < 8; ++i)
          imei.append(source[i]);


          qlonglong val = 0;
          for(int i = 0; i < 8; ++i)
          val *= 256;
          val += (quint8)imei.at(i);

          qDebug() << QString::number(val);





          share|improve this answer




















          • That did the trick. Thank you so much! I was trying to shift the number instead of multiplying.
            – user1298272
            Nov 11 at 8:00










          • Shifting for long long is not available, I got the same problem with you.
            – 张铁男
            Nov 11 at 9:25














          up vote
          1
          down vote



          accepted










          quint8 source[8] = 0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3;
          QByteArray imei;
          for(int i = 0; i < 8; ++i)
          imei.append(source[i]);


          qlonglong val = 0;
          for(int i = 0; i < 8; ++i)
          val *= 256;
          val += (quint8)imei.at(i);

          qDebug() << QString::number(val);





          share|improve this answer




















          • That did the trick. Thank you so much! I was trying to shift the number instead of multiplying.
            – user1298272
            Nov 11 at 8:00










          • Shifting for long long is not available, I got the same problem with you.
            – 张铁男
            Nov 11 at 9:25












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          quint8 source[8] = 0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3;
          QByteArray imei;
          for(int i = 0; i < 8; ++i)
          imei.append(source[i]);


          qlonglong val = 0;
          for(int i = 0; i < 8; ++i)
          val *= 256;
          val += (quint8)imei.at(i);

          qDebug() << QString::number(val);





          share|improve this answer












          quint8 source[8] = 0x00, 0x01, 0x40, 0x76, 0xA0, 0xB0, 0x85, 0xA3;
          QByteArray imei;
          for(int i = 0; i < 8; ++i)
          imei.append(source[i]);


          qlonglong val = 0;
          for(int i = 0; i < 8; ++i)
          val *= 256;
          val += (quint8)imei.at(i);

          qDebug() << QString::number(val);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 23:06









          张铁男

          262




          262











          • That did the trick. Thank you so much! I was trying to shift the number instead of multiplying.
            – user1298272
            Nov 11 at 8:00










          • Shifting for long long is not available, I got the same problem with you.
            – 张铁男
            Nov 11 at 9:25
















          • That did the trick. Thank you so much! I was trying to shift the number instead of multiplying.
            – user1298272
            Nov 11 at 8:00










          • Shifting for long long is not available, I got the same problem with you.
            – 张铁男
            Nov 11 at 9:25















          That did the trick. Thank you so much! I was trying to shift the number instead of multiplying.
          – user1298272
          Nov 11 at 8:00




          That did the trick. Thank you so much! I was trying to shift the number instead of multiplying.
          – user1298272
          Nov 11 at 8:00












          Shifting for long long is not available, I got the same problem with you.
          – 张铁男
          Nov 11 at 9:25




          Shifting for long long is not available, I got the same problem with you.
          – 张铁男
          Nov 11 at 9:25












          up vote
          1
          down vote













          You can use stringstreams:



          #include <sstream>

          QString ConvertToQString(const QByteArray& array)

          std::stringstream ss;
          for (int i = 0; i < array.size(); ++i)
          ss << array[i];

          return QString::fromStdString(ss.str());






          share|improve this answer






















          • Thanks. I get an error at the return. "no matching conversion for functional-style cast form..." I guess QString does not have that converter. I will check
            – user1298272
            Nov 10 at 17:22










          • Yes, you're right. I've modified the code. Please check this out.
            – Peter
            Nov 10 at 18:47










          • I just added some information about your function. Please, check it.
            – user1298272
            Nov 10 at 20:28














          up vote
          1
          down vote













          You can use stringstreams:



          #include <sstream>

          QString ConvertToQString(const QByteArray& array)

          std::stringstream ss;
          for (int i = 0; i < array.size(); ++i)
          ss << array[i];

          return QString::fromStdString(ss.str());






          share|improve this answer






















          • Thanks. I get an error at the return. "no matching conversion for functional-style cast form..." I guess QString does not have that converter. I will check
            – user1298272
            Nov 10 at 17:22










          • Yes, you're right. I've modified the code. Please check this out.
            – Peter
            Nov 10 at 18:47










          • I just added some information about your function. Please, check it.
            – user1298272
            Nov 10 at 20:28












          up vote
          1
          down vote










          up vote
          1
          down vote









          You can use stringstreams:



          #include <sstream>

          QString ConvertToQString(const QByteArray& array)

          std::stringstream ss;
          for (int i = 0; i < array.size(); ++i)
          ss << array[i];

          return QString::fromStdString(ss.str());






          share|improve this answer














          You can use stringstreams:



          #include <sstream>

          QString ConvertToQString(const QByteArray& array)

          std::stringstream ss;
          for (int i = 0; i < array.size(); ++i)
          ss << array[i];

          return QString::fromStdString(ss.str());







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 18:46

























          answered Nov 10 at 17:18









          Peter

          59618




          59618











          • Thanks. I get an error at the return. "no matching conversion for functional-style cast form..." I guess QString does not have that converter. I will check
            – user1298272
            Nov 10 at 17:22










          • Yes, you're right. I've modified the code. Please check this out.
            – Peter
            Nov 10 at 18:47










          • I just added some information about your function. Please, check it.
            – user1298272
            Nov 10 at 20:28
















          • Thanks. I get an error at the return. "no matching conversion for functional-style cast form..." I guess QString does not have that converter. I will check
            – user1298272
            Nov 10 at 17:22










          • Yes, you're right. I've modified the code. Please check this out.
            – Peter
            Nov 10 at 18:47










          • I just added some information about your function. Please, check it.
            – user1298272
            Nov 10 at 20:28















          Thanks. I get an error at the return. "no matching conversion for functional-style cast form..." I guess QString does not have that converter. I will check
          – user1298272
          Nov 10 at 17:22




          Thanks. I get an error at the return. "no matching conversion for functional-style cast form..." I guess QString does not have that converter. I will check
          – user1298272
          Nov 10 at 17:22












          Yes, you're right. I've modified the code. Please check this out.
          – Peter
          Nov 10 at 18:47




          Yes, you're right. I've modified the code. Please check this out.
          – Peter
          Nov 10 at 18:47












          I just added some information about your function. Please, check it.
          – user1298272
          Nov 10 at 20:28




          I just added some information about your function. Please, check it.
          – user1298272
          Nov 10 at 20:28

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241306%2fget-imei-from-qbytearray-in-hex%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto