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 |
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
张铁男
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
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
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
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 |
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