Add(not concatenate) a number to QByteArray
I have the following QByteArray
:
QByteArray ba;
ba.resize(3);
ba[0]=ba[2]=0x8a;
ba[1]=0x0d; //so ba=8a0d8a
I want to add a hexadecimal number to the above QByteArray
. For eg. on adding 0x01, ba
should contain 8a0d8b
. Any such operation involving carry should be propagated forward as in a normal hex addition. I have tried using +
operator:
ba=ba+1;
but it concatenates (resulting in 8a0d8a01
in the above case) instead of performing actual operation. How can this be done ?
c++ qt qt5 qbytearray
|
show 3 more comments
I have the following QByteArray
:
QByteArray ba;
ba.resize(3);
ba[0]=ba[2]=0x8a;
ba[1]=0x0d; //so ba=8a0d8a
I want to add a hexadecimal number to the above QByteArray
. For eg. on adding 0x01, ba
should contain 8a0d8b
. Any such operation involving carry should be propagated forward as in a normal hex addition. I have tried using +
operator:
ba=ba+1;
but it concatenates (resulting in 8a0d8a01
in the above case) instead of performing actual operation. How can this be done ?
c++ qt qt5 qbytearray
Should the byte array always have three elements?
– vahancho
Nov 16 '18 at 8:26
It can have 3 or 4 elements. Not more
– Abhishek Agarwal
Nov 16 '18 at 8:27
But maybe it's better to write a class that will store and operate with numbers directly and return them as a byte array if requested?
– vahancho
Nov 16 '18 at 8:29
I was hoping for some inbuilt function to do it. Else I can go for the more manual byte wise addition inside a for loop but that means unnecessarily complicating the code
– Abhishek Agarwal
Nov 16 '18 at 8:32
2
Do you really need make math on QByteArray? Why not to do this with lets say long and shift to needed position?
– Xplatforms
Nov 16 '18 at 8:54
|
show 3 more comments
I have the following QByteArray
:
QByteArray ba;
ba.resize(3);
ba[0]=ba[2]=0x8a;
ba[1]=0x0d; //so ba=8a0d8a
I want to add a hexadecimal number to the above QByteArray
. For eg. on adding 0x01, ba
should contain 8a0d8b
. Any such operation involving carry should be propagated forward as in a normal hex addition. I have tried using +
operator:
ba=ba+1;
but it concatenates (resulting in 8a0d8a01
in the above case) instead of performing actual operation. How can this be done ?
c++ qt qt5 qbytearray
I have the following QByteArray
:
QByteArray ba;
ba.resize(3);
ba[0]=ba[2]=0x8a;
ba[1]=0x0d; //so ba=8a0d8a
I want to add a hexadecimal number to the above QByteArray
. For eg. on adding 0x01, ba
should contain 8a0d8b
. Any such operation involving carry should be propagated forward as in a normal hex addition. I have tried using +
operator:
ba=ba+1;
but it concatenates (resulting in 8a0d8a01
in the above case) instead of performing actual operation. How can this be done ?
c++ qt qt5 qbytearray
c++ qt qt5 qbytearray
asked Nov 16 '18 at 8:23
Abhishek AgarwalAbhishek Agarwal
5451026
5451026
Should the byte array always have three elements?
– vahancho
Nov 16 '18 at 8:26
It can have 3 or 4 elements. Not more
– Abhishek Agarwal
Nov 16 '18 at 8:27
But maybe it's better to write a class that will store and operate with numbers directly and return them as a byte array if requested?
– vahancho
Nov 16 '18 at 8:29
I was hoping for some inbuilt function to do it. Else I can go for the more manual byte wise addition inside a for loop but that means unnecessarily complicating the code
– Abhishek Agarwal
Nov 16 '18 at 8:32
2
Do you really need make math on QByteArray? Why not to do this with lets say long and shift to needed position?
– Xplatforms
Nov 16 '18 at 8:54
|
show 3 more comments
Should the byte array always have three elements?
– vahancho
Nov 16 '18 at 8:26
It can have 3 or 4 elements. Not more
– Abhishek Agarwal
Nov 16 '18 at 8:27
But maybe it's better to write a class that will store and operate with numbers directly and return them as a byte array if requested?
– vahancho
Nov 16 '18 at 8:29
I was hoping for some inbuilt function to do it. Else I can go for the more manual byte wise addition inside a for loop but that means unnecessarily complicating the code
– Abhishek Agarwal
Nov 16 '18 at 8:32
2
Do you really need make math on QByteArray? Why not to do this with lets say long and shift to needed position?
– Xplatforms
Nov 16 '18 at 8:54
Should the byte array always have three elements?
– vahancho
Nov 16 '18 at 8:26
Should the byte array always have three elements?
– vahancho
Nov 16 '18 at 8:26
It can have 3 or 4 elements. Not more
– Abhishek Agarwal
Nov 16 '18 at 8:27
It can have 3 or 4 elements. Not more
– Abhishek Agarwal
Nov 16 '18 at 8:27
But maybe it's better to write a class that will store and operate with numbers directly and return them as a byte array if requested?
– vahancho
Nov 16 '18 at 8:29
But maybe it's better to write a class that will store and operate with numbers directly and return them as a byte array if requested?
– vahancho
Nov 16 '18 at 8:29
I was hoping for some inbuilt function to do it. Else I can go for the more manual byte wise addition inside a for loop but that means unnecessarily complicating the code
– Abhishek Agarwal
Nov 16 '18 at 8:32
I was hoping for some inbuilt function to do it. Else I can go for the more manual byte wise addition inside a for loop but that means unnecessarily complicating the code
– Abhishek Agarwal
Nov 16 '18 at 8:32
2
2
Do you really need make math on QByteArray? Why not to do this with lets say long and shift to needed position?
– Xplatforms
Nov 16 '18 at 8:54
Do you really need make math on QByteArray? Why not to do this with lets say long and shift to needed position?
– Xplatforms
Nov 16 '18 at 8:54
|
show 3 more comments
3 Answers
3
active
oldest
votes
I think this is the easiest solution:
uint32_t num = (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]);
num++; // add one
b[0] = num & 0xff;
b[1] = (num >> 8) & 0xff;
b[2] = (num >> 16) & 0xff;
b[3] = (num >> 24) & 0xff;
Basically you convert to an arithmetic integer back and forth. Just make sure you don't overflow. This is a simple example. You can make it a class with a state that returns QByteArray
on some method or you can make a function that does this once at a time.
A simple solution, as long as endianness of the data does not matter
– Felix
Nov 16 '18 at 12:13
@Felix The op provided big endian data, which is all we care about. You're introducing a problem that doesn't exist.
– The Quantum Physicist
Nov 16 '18 at 14:15
add a comment |
First off: There is no such functionality in QByteArray. There are ways around it however, as long as you know how long the bytearray will be. There is however one tiny thing that makes this extra complicated: Endianess.
The following example assumes all your bytearrays have a maximum length. The function is generic, so you can decide dynamically if required:
template <typename TInt>
void addNumber(QByteArray &data, TInt number) {
static_assert(std::is_integral<TInt>::value, "TInt must be an integral type");
constexpr auto maxSize = sizeof(TInt);
const auto dSize = data.size();
// make shure no data that is longer then the allowed integer size is passed
Q_ASSERT_X(dSize <= maxSize, Q_FUNC_INFO, "bytearrays longer than sizeof(TInt) byte are not allowed!");
// prepend '' bytes to the array to fill it up to an N byte length
if(dSize < maxSize)
data = QByteArray{maxSize - dSize, ''} + data;
// convert to a number, add and convert back
auto dataNum = qFromBigEndian<TInt>(data.constData());
dataNum += number;
qToBigEndian(dataNum, data.data());
// resize the data back to the original size, dropping the previously prepended bytes
if(dSize < maxSize)
data = data.mid(maxSize - dSize);
}
To use the method, simply find out how long your bytearrays can be and then use the method with the according type. For example, if your data is limited to at most 4 bytes (as specified in the commencts), you could use it as:
QByteArray ba = "x8ax0dx8a";
addNumber<quint32>(ba, 1u);
// ba is now "x8ax0dx8b"
Important: Be careful with the template argument. specify it explicitly to ensure you do not accidentially pass a literal of a different type. For example, calling it as simply addNumber(ba, 1)
will deduce TInt
as int
- not an unsinged int.
EDIT:
In case you define your bytearrays endianness to be the same as the current plattform, no such conversion is needed. The code would change to:
// convert to a number, add and convert back
*(reinterpret_cast<TInt*>(data.data())) += number;
It really depends on where this data comes from and how you use it.
There are no endianness problems here... it's a byte array.
– The Quantum Physicist
Nov 16 '18 at 10:15
@TheQuantumPhysicist the number has an endianness defined by the platform, the bytearray has an endianness defined by OP. They might differ
– Caleth
Nov 16 '18 at 10:49
@Caleth Look at my solution and see how endianness doesn't matter unless you make it.
– The Quantum Physicist
Nov 16 '18 at 10:54
@TheQuantumPhysicist "It can have 3 or 4 elements" You answer assumes 4. This does not.
– Caleth
Nov 16 '18 at 10:56
@Caleth And why does it matter for endianness whether it's 3 or 4? My answer works for both because the forth byte can be zero. This is a proof of concept. You can make it all kinds of fancy by putting it in a loop.
– The Quantum Physicist
Nov 16 '18 at 10:56
|
show 2 more comments
Why not use a simple data like
template <typename T> union BData
{
uint8_t data[sizeof(T)];
T value;
};
Then you can perform your arithmetic operations and extract bytes from the union. However you should be aware of endiness.
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',
autoActivateHeartbeat: false,
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%2f53333963%2faddnot-concatenate-a-number-to-qbytearray%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think this is the easiest solution:
uint32_t num = (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]);
num++; // add one
b[0] = num & 0xff;
b[1] = (num >> 8) & 0xff;
b[2] = (num >> 16) & 0xff;
b[3] = (num >> 24) & 0xff;
Basically you convert to an arithmetic integer back and forth. Just make sure you don't overflow. This is a simple example. You can make it a class with a state that returns QByteArray
on some method or you can make a function that does this once at a time.
A simple solution, as long as endianness of the data does not matter
– Felix
Nov 16 '18 at 12:13
@Felix The op provided big endian data, which is all we care about. You're introducing a problem that doesn't exist.
– The Quantum Physicist
Nov 16 '18 at 14:15
add a comment |
I think this is the easiest solution:
uint32_t num = (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]);
num++; // add one
b[0] = num & 0xff;
b[1] = (num >> 8) & 0xff;
b[2] = (num >> 16) & 0xff;
b[3] = (num >> 24) & 0xff;
Basically you convert to an arithmetic integer back and forth. Just make sure you don't overflow. This is a simple example. You can make it a class with a state that returns QByteArray
on some method or you can make a function that does this once at a time.
A simple solution, as long as endianness of the data does not matter
– Felix
Nov 16 '18 at 12:13
@Felix The op provided big endian data, which is all we care about. You're introducing a problem that doesn't exist.
– The Quantum Physicist
Nov 16 '18 at 14:15
add a comment |
I think this is the easiest solution:
uint32_t num = (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]);
num++; // add one
b[0] = num & 0xff;
b[1] = (num >> 8) & 0xff;
b[2] = (num >> 16) & 0xff;
b[3] = (num >> 24) & 0xff;
Basically you convert to an arithmetic integer back and forth. Just make sure you don't overflow. This is a simple example. You can make it a class with a state that returns QByteArray
on some method or you can make a function that does this once at a time.
I think this is the easiest solution:
uint32_t num = (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]);
num++; // add one
b[0] = num & 0xff;
b[1] = (num >> 8) & 0xff;
b[2] = (num >> 16) & 0xff;
b[3] = (num >> 24) & 0xff;
Basically you convert to an arithmetic integer back and forth. Just make sure you don't overflow. This is a simple example. You can make it a class with a state that returns QByteArray
on some method or you can make a function that does this once at a time.
edited Nov 16 '18 at 10:25
answered Nov 16 '18 at 10:19
The Quantum PhysicistThe Quantum Physicist
12.2k748103
12.2k748103
A simple solution, as long as endianness of the data does not matter
– Felix
Nov 16 '18 at 12:13
@Felix The op provided big endian data, which is all we care about. You're introducing a problem that doesn't exist.
– The Quantum Physicist
Nov 16 '18 at 14:15
add a comment |
A simple solution, as long as endianness of the data does not matter
– Felix
Nov 16 '18 at 12:13
@Felix The op provided big endian data, which is all we care about. You're introducing a problem that doesn't exist.
– The Quantum Physicist
Nov 16 '18 at 14:15
A simple solution, as long as endianness of the data does not matter
– Felix
Nov 16 '18 at 12:13
A simple solution, as long as endianness of the data does not matter
– Felix
Nov 16 '18 at 12:13
@Felix The op provided big endian data, which is all we care about. You're introducing a problem that doesn't exist.
– The Quantum Physicist
Nov 16 '18 at 14:15
@Felix The op provided big endian data, which is all we care about. You're introducing a problem that doesn't exist.
– The Quantum Physicist
Nov 16 '18 at 14:15
add a comment |
First off: There is no such functionality in QByteArray. There are ways around it however, as long as you know how long the bytearray will be. There is however one tiny thing that makes this extra complicated: Endianess.
The following example assumes all your bytearrays have a maximum length. The function is generic, so you can decide dynamically if required:
template <typename TInt>
void addNumber(QByteArray &data, TInt number) {
static_assert(std::is_integral<TInt>::value, "TInt must be an integral type");
constexpr auto maxSize = sizeof(TInt);
const auto dSize = data.size();
// make shure no data that is longer then the allowed integer size is passed
Q_ASSERT_X(dSize <= maxSize, Q_FUNC_INFO, "bytearrays longer than sizeof(TInt) byte are not allowed!");
// prepend '' bytes to the array to fill it up to an N byte length
if(dSize < maxSize)
data = QByteArray{maxSize - dSize, ''} + data;
// convert to a number, add and convert back
auto dataNum = qFromBigEndian<TInt>(data.constData());
dataNum += number;
qToBigEndian(dataNum, data.data());
// resize the data back to the original size, dropping the previously prepended bytes
if(dSize < maxSize)
data = data.mid(maxSize - dSize);
}
To use the method, simply find out how long your bytearrays can be and then use the method with the according type. For example, if your data is limited to at most 4 bytes (as specified in the commencts), you could use it as:
QByteArray ba = "x8ax0dx8a";
addNumber<quint32>(ba, 1u);
// ba is now "x8ax0dx8b"
Important: Be careful with the template argument. specify it explicitly to ensure you do not accidentially pass a literal of a different type. For example, calling it as simply addNumber(ba, 1)
will deduce TInt
as int
- not an unsinged int.
EDIT:
In case you define your bytearrays endianness to be the same as the current plattform, no such conversion is needed. The code would change to:
// convert to a number, add and convert back
*(reinterpret_cast<TInt*>(data.data())) += number;
It really depends on where this data comes from and how you use it.
There are no endianness problems here... it's a byte array.
– The Quantum Physicist
Nov 16 '18 at 10:15
@TheQuantumPhysicist the number has an endianness defined by the platform, the bytearray has an endianness defined by OP. They might differ
– Caleth
Nov 16 '18 at 10:49
@Caleth Look at my solution and see how endianness doesn't matter unless you make it.
– The Quantum Physicist
Nov 16 '18 at 10:54
@TheQuantumPhysicist "It can have 3 or 4 elements" You answer assumes 4. This does not.
– Caleth
Nov 16 '18 at 10:56
@Caleth And why does it matter for endianness whether it's 3 or 4? My answer works for both because the forth byte can be zero. This is a proof of concept. You can make it all kinds of fancy by putting it in a loop.
– The Quantum Physicist
Nov 16 '18 at 10:56
|
show 2 more comments
First off: There is no such functionality in QByteArray. There are ways around it however, as long as you know how long the bytearray will be. There is however one tiny thing that makes this extra complicated: Endianess.
The following example assumes all your bytearrays have a maximum length. The function is generic, so you can decide dynamically if required:
template <typename TInt>
void addNumber(QByteArray &data, TInt number) {
static_assert(std::is_integral<TInt>::value, "TInt must be an integral type");
constexpr auto maxSize = sizeof(TInt);
const auto dSize = data.size();
// make shure no data that is longer then the allowed integer size is passed
Q_ASSERT_X(dSize <= maxSize, Q_FUNC_INFO, "bytearrays longer than sizeof(TInt) byte are not allowed!");
// prepend '' bytes to the array to fill it up to an N byte length
if(dSize < maxSize)
data = QByteArray{maxSize - dSize, ''} + data;
// convert to a number, add and convert back
auto dataNum = qFromBigEndian<TInt>(data.constData());
dataNum += number;
qToBigEndian(dataNum, data.data());
// resize the data back to the original size, dropping the previously prepended bytes
if(dSize < maxSize)
data = data.mid(maxSize - dSize);
}
To use the method, simply find out how long your bytearrays can be and then use the method with the according type. For example, if your data is limited to at most 4 bytes (as specified in the commencts), you could use it as:
QByteArray ba = "x8ax0dx8a";
addNumber<quint32>(ba, 1u);
// ba is now "x8ax0dx8b"
Important: Be careful with the template argument. specify it explicitly to ensure you do not accidentially pass a literal of a different type. For example, calling it as simply addNumber(ba, 1)
will deduce TInt
as int
- not an unsinged int.
EDIT:
In case you define your bytearrays endianness to be the same as the current plattform, no such conversion is needed. The code would change to:
// convert to a number, add and convert back
*(reinterpret_cast<TInt*>(data.data())) += number;
It really depends on where this data comes from and how you use it.
There are no endianness problems here... it's a byte array.
– The Quantum Physicist
Nov 16 '18 at 10:15
@TheQuantumPhysicist the number has an endianness defined by the platform, the bytearray has an endianness defined by OP. They might differ
– Caleth
Nov 16 '18 at 10:49
@Caleth Look at my solution and see how endianness doesn't matter unless you make it.
– The Quantum Physicist
Nov 16 '18 at 10:54
@TheQuantumPhysicist "It can have 3 or 4 elements" You answer assumes 4. This does not.
– Caleth
Nov 16 '18 at 10:56
@Caleth And why does it matter for endianness whether it's 3 or 4? My answer works for both because the forth byte can be zero. This is a proof of concept. You can make it all kinds of fancy by putting it in a loop.
– The Quantum Physicist
Nov 16 '18 at 10:56
|
show 2 more comments
First off: There is no such functionality in QByteArray. There are ways around it however, as long as you know how long the bytearray will be. There is however one tiny thing that makes this extra complicated: Endianess.
The following example assumes all your bytearrays have a maximum length. The function is generic, so you can decide dynamically if required:
template <typename TInt>
void addNumber(QByteArray &data, TInt number) {
static_assert(std::is_integral<TInt>::value, "TInt must be an integral type");
constexpr auto maxSize = sizeof(TInt);
const auto dSize = data.size();
// make shure no data that is longer then the allowed integer size is passed
Q_ASSERT_X(dSize <= maxSize, Q_FUNC_INFO, "bytearrays longer than sizeof(TInt) byte are not allowed!");
// prepend '' bytes to the array to fill it up to an N byte length
if(dSize < maxSize)
data = QByteArray{maxSize - dSize, ''} + data;
// convert to a number, add and convert back
auto dataNum = qFromBigEndian<TInt>(data.constData());
dataNum += number;
qToBigEndian(dataNum, data.data());
// resize the data back to the original size, dropping the previously prepended bytes
if(dSize < maxSize)
data = data.mid(maxSize - dSize);
}
To use the method, simply find out how long your bytearrays can be and then use the method with the according type. For example, if your data is limited to at most 4 bytes (as specified in the commencts), you could use it as:
QByteArray ba = "x8ax0dx8a";
addNumber<quint32>(ba, 1u);
// ba is now "x8ax0dx8b"
Important: Be careful with the template argument. specify it explicitly to ensure you do not accidentially pass a literal of a different type. For example, calling it as simply addNumber(ba, 1)
will deduce TInt
as int
- not an unsinged int.
EDIT:
In case you define your bytearrays endianness to be the same as the current plattform, no such conversion is needed. The code would change to:
// convert to a number, add and convert back
*(reinterpret_cast<TInt*>(data.data())) += number;
It really depends on where this data comes from and how you use it.
First off: There is no such functionality in QByteArray. There are ways around it however, as long as you know how long the bytearray will be. There is however one tiny thing that makes this extra complicated: Endianess.
The following example assumes all your bytearrays have a maximum length. The function is generic, so you can decide dynamically if required:
template <typename TInt>
void addNumber(QByteArray &data, TInt number) {
static_assert(std::is_integral<TInt>::value, "TInt must be an integral type");
constexpr auto maxSize = sizeof(TInt);
const auto dSize = data.size();
// make shure no data that is longer then the allowed integer size is passed
Q_ASSERT_X(dSize <= maxSize, Q_FUNC_INFO, "bytearrays longer than sizeof(TInt) byte are not allowed!");
// prepend '' bytes to the array to fill it up to an N byte length
if(dSize < maxSize)
data = QByteArray{maxSize - dSize, ''} + data;
// convert to a number, add and convert back
auto dataNum = qFromBigEndian<TInt>(data.constData());
dataNum += number;
qToBigEndian(dataNum, data.data());
// resize the data back to the original size, dropping the previously prepended bytes
if(dSize < maxSize)
data = data.mid(maxSize - dSize);
}
To use the method, simply find out how long your bytearrays can be and then use the method with the according type. For example, if your data is limited to at most 4 bytes (as specified in the commencts), you could use it as:
QByteArray ba = "x8ax0dx8a";
addNumber<quint32>(ba, 1u);
// ba is now "x8ax0dx8b"
Important: Be careful with the template argument. specify it explicitly to ensure you do not accidentially pass a literal of a different type. For example, calling it as simply addNumber(ba, 1)
will deduce TInt
as int
- not an unsinged int.
EDIT:
In case you define your bytearrays endianness to be the same as the current plattform, no such conversion is needed. The code would change to:
// convert to a number, add and convert back
*(reinterpret_cast<TInt*>(data.data())) += number;
It really depends on where this data comes from and how you use it.
edited Nov 16 '18 at 12:12
answered Nov 16 '18 at 10:10
FelixFelix
4,78011941
4,78011941
There are no endianness problems here... it's a byte array.
– The Quantum Physicist
Nov 16 '18 at 10:15
@TheQuantumPhysicist the number has an endianness defined by the platform, the bytearray has an endianness defined by OP. They might differ
– Caleth
Nov 16 '18 at 10:49
@Caleth Look at my solution and see how endianness doesn't matter unless you make it.
– The Quantum Physicist
Nov 16 '18 at 10:54
@TheQuantumPhysicist "It can have 3 or 4 elements" You answer assumes 4. This does not.
– Caleth
Nov 16 '18 at 10:56
@Caleth And why does it matter for endianness whether it's 3 or 4? My answer works for both because the forth byte can be zero. This is a proof of concept. You can make it all kinds of fancy by putting it in a loop.
– The Quantum Physicist
Nov 16 '18 at 10:56
|
show 2 more comments
There are no endianness problems here... it's a byte array.
– The Quantum Physicist
Nov 16 '18 at 10:15
@TheQuantumPhysicist the number has an endianness defined by the platform, the bytearray has an endianness defined by OP. They might differ
– Caleth
Nov 16 '18 at 10:49
@Caleth Look at my solution and see how endianness doesn't matter unless you make it.
– The Quantum Physicist
Nov 16 '18 at 10:54
@TheQuantumPhysicist "It can have 3 or 4 elements" You answer assumes 4. This does not.
– Caleth
Nov 16 '18 at 10:56
@Caleth And why does it matter for endianness whether it's 3 or 4? My answer works for both because the forth byte can be zero. This is a proof of concept. You can make it all kinds of fancy by putting it in a loop.
– The Quantum Physicist
Nov 16 '18 at 10:56
There are no endianness problems here... it's a byte array.
– The Quantum Physicist
Nov 16 '18 at 10:15
There are no endianness problems here... it's a byte array.
– The Quantum Physicist
Nov 16 '18 at 10:15
@TheQuantumPhysicist the number has an endianness defined by the platform, the bytearray has an endianness defined by OP. They might differ
– Caleth
Nov 16 '18 at 10:49
@TheQuantumPhysicist the number has an endianness defined by the platform, the bytearray has an endianness defined by OP. They might differ
– Caleth
Nov 16 '18 at 10:49
@Caleth Look at my solution and see how endianness doesn't matter unless you make it.
– The Quantum Physicist
Nov 16 '18 at 10:54
@Caleth Look at my solution and see how endianness doesn't matter unless you make it.
– The Quantum Physicist
Nov 16 '18 at 10:54
@TheQuantumPhysicist "It can have 3 or 4 elements" You answer assumes 4. This does not.
– Caleth
Nov 16 '18 at 10:56
@TheQuantumPhysicist "It can have 3 or 4 elements" You answer assumes 4. This does not.
– Caleth
Nov 16 '18 at 10:56
@Caleth And why does it matter for endianness whether it's 3 or 4? My answer works for both because the forth byte can be zero. This is a proof of concept. You can make it all kinds of fancy by putting it in a loop.
– The Quantum Physicist
Nov 16 '18 at 10:56
@Caleth And why does it matter for endianness whether it's 3 or 4? My answer works for both because the forth byte can be zero. This is a proof of concept. You can make it all kinds of fancy by putting it in a loop.
– The Quantum Physicist
Nov 16 '18 at 10:56
|
show 2 more comments
Why not use a simple data like
template <typename T> union BData
{
uint8_t data[sizeof(T)];
T value;
};
Then you can perform your arithmetic operations and extract bytes from the union. However you should be aware of endiness.
add a comment |
Why not use a simple data like
template <typename T> union BData
{
uint8_t data[sizeof(T)];
T value;
};
Then you can perform your arithmetic operations and extract bytes from the union. However you should be aware of endiness.
add a comment |
Why not use a simple data like
template <typename T> union BData
{
uint8_t data[sizeof(T)];
T value;
};
Then you can perform your arithmetic operations and extract bytes from the union. However you should be aware of endiness.
Why not use a simple data like
template <typename T> union BData
{
uint8_t data[sizeof(T)];
T value;
};
Then you can perform your arithmetic operations and extract bytes from the union. However you should be aware of endiness.
answered Nov 16 '18 at 12:33
Ilian ZapryanovIlian Zapryanov
3951515
3951515
add a comment |
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.
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%2f53333963%2faddnot-concatenate-a-number-to-qbytearray%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
Should the byte array always have three elements?
– vahancho
Nov 16 '18 at 8:26
It can have 3 or 4 elements. Not more
– Abhishek Agarwal
Nov 16 '18 at 8:27
But maybe it's better to write a class that will store and operate with numbers directly and return them as a byte array if requested?
– vahancho
Nov 16 '18 at 8:29
I was hoping for some inbuilt function to do it. Else I can go for the more manual byte wise addition inside a for loop but that means unnecessarily complicating the code
– Abhishek Agarwal
Nov 16 '18 at 8:32
2
Do you really need make math on QByteArray? Why not to do this with lets say long and shift to needed position?
– Xplatforms
Nov 16 '18 at 8:54