QByteArray increment, and qDebug() benaviour
-
Hi
A very basic one .. I just can't find it ;-)
Is it just me that is missing something or is it not possible to increment in a QByteArray?
qbTest[iIdx]+=1;
If possible, how?
Or do I better use a standard C++ array?Something else I just can't find why:
If qbTest[iIdx] is = 1, qDebug() does output nothing - which is OK I assume because 0x01 is 'not visible'.
If I add '0' it should be 0x31 which is '1', but it outputs 49 - I regularly use this in C without issues.
If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'
Can someone shed some light on this?Thanks
-
Hi
A very basic one .. I just can't find it ;-)
Is it just me that is missing something or is it not possible to increment in a QByteArray?
qbTest[iIdx]+=1;
If possible, how?
Or do I better use a standard C++ array?Something else I just can't find why:
If qbTest[iIdx] is = 1, qDebug() does output nothing - which is OK I assume because 0x01 is 'not visible'.
If I add '0' it should be 0x31 which is '1', but it outputs 49 - I regularly use this in C without issues.
If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'
Can someone shed some light on this?Thanks
@McLion said in QByteArray increment:
Is it just me that is missing something or is it not possible to increment in a QByteArray?
i think you are just assigning your value to a copy and thus it gets lost.
Does the following work?
qbTest[iIdx] = char(qbTest[iIdx]) + 1;
-
@McLion said in QByteArray increment:
Is it just me that is missing something or is it not possible to increment in a QByteArray?
i think you are just assigning your value to a copy and thus it gets lost.
Does the following work?
qbTest[iIdx] = char(qbTest[iIdx]) + 1;
-
@McLion said in QByteArray increment:
Is it just me that is missing something or is it not possible to increment in a QByteArray?
i think you are just assigning your value to a copy and thus it gets lost.
Does the following work?
qbTest[iIdx] = char(qbTest[iIdx]) + 1;
@raven-worx
+= as well as ++ returns a 'no match for operator...' -
@raven-worx
+= as well as ++ returns a 'no match for operator...'@McLion
QByteArray's[]
operator returns a QByteRef (which is unfortunately not documented, but available via public API)
Honestly i am not quite sure why the+
even works, since the QByteRef class hasn't defined it. But i guess the compiler implicitly converts it tochar
maybe -
@raven-worx
Thanks. I implemented it the (longer) way it works.Anybody an idea about the Debug() question?
Sorry I put 2 issues in one thread ;-) -
@raven-worx
Thanks. I implemented it the (longer) way it works.Anybody an idea about the Debug() question?
Sorry I put 2 issues in one thread ;-)@McLion said in QByteArray increment, and qDebug() benaviour:
Anybody an idea about the Debug() question?
try passing
char
/char*
to qDebug(), instead of the Qt classes. -
@McLion said in QByteArray increment, and qDebug() benaviour:
Anybody an idea about the Debug() question?
try passing
char
/char*
to qDebug(), instead of the Qt classes.Didn't help ... never mind.
-
@raven-worx
Thanks. I implemented it the (longer) way it works.Anybody an idea about the Debug() question?
Sorry I put 2 issues in one thread ;-)@McLion said in QByteArray increment, and qDebug() benaviour:
I regularly use this in C without issues.
How do you print it in C? Note:
printf("%d\n", '1')
prints "49"printf("%c\n", 49)
prints "1"
Similarly, QDebug formats
char
values using%c
and formatsint
values using%d
.Note also, when you add/subtract
char
values, the compiler (at least MSVC 2013 and GCC 4.8) implicitly casts them toint
. Proof:// Input: #include <typeinfo> ... qDebug() << typeid('0' + 1).name(); qDebug() << typeid('0' - '0').name(); // Output (on MSVC 2013 and GCC 4.8): int int
If I add '0' it should be 0x31 which is '1', but it outputs 49
QDebug receives
(int)0x31
, so it prints "49".If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'
Your code is
1 + '0' - 48
==1 + 48 - 48
==1
.QDebug receives a
(int)0x01
, so it prints "1". -
@McLion said in QByteArray increment, and qDebug() benaviour:
I regularly use this in C without issues.
How do you print it in C? Note:
printf("%d\n", '1')
prints "49"printf("%c\n", 49)
prints "1"
Similarly, QDebug formats
char
values using%c
and formatsint
values using%d
.Note also, when you add/subtract
char
values, the compiler (at least MSVC 2013 and GCC 4.8) implicitly casts them toint
. Proof:// Input: #include <typeinfo> ... qDebug() << typeid('0' + 1).name(); qDebug() << typeid('0' - '0').name(); // Output (on MSVC 2013 and GCC 4.8): int int
If I add '0' it should be 0x31 which is '1', but it outputs 49
QDebug receives
(int)0x31
, so it prints "49".If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'
Your code is
1 + '0' - 48
==1 + 48 - 48
==1
.QDebug receives a
(int)0x01
, so it prints "1".