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:
imeiStr is shorter and also, from position 4, differs from the original.
c++ qt
add a comment |
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:
imeiStr is shorter and also, from position 4, differs from the original.
c++ qt
add a comment |
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:
imeiStr is shorter and also, from position 4, differs from the original.
c++ qt
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:
imeiStr is shorter and also, from position 4, differs from the original.
c++ qt
c++ qt
edited Nov 10 at 20:27
asked Nov 10 at 17:03
user1298272
182424
182424
add a comment |
add a comment |
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);
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
add a comment |
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());
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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);
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
add a comment |
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);
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
add a comment |
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);
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);
answered Nov 10 at 23:06
张铁男
745
745
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
add a comment |
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
add a comment |
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());
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
add a comment |
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());
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
add a comment |
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());
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());
edited Nov 10 at 18:46
answered Nov 10 at 17:18
Peter
62618
62618
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
add a comment |
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
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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