PDA

View Full Version : QByteArray sample in doc error!



yandi
31st January 2012, 08:19
Hello,

I read in Qbytesarray doc and try this code,

QByteArray ba;
ba.resize(5);
ba[0] = 0x3c;
ba[1] = 0xb8; ->
ba[2] = 0x64;
ba[3] = 0x18;
ba[4] = 0xca;->

but appear it's show error and can only input until "0x7f" or 127, how if I want to input data for example "0xff".


thanks

wysota
31st January 2012, 10:38
Works for me...


#include <QtDebug>
#include <QByteArray>
#include <stdio.h>

int main(int argc, char **argv) {
QByteArray ba = "\x3c\xb8\x64\x18\xca";
for(int i=0;i<ba.size();++i) {
printf("%d: %x\n", i, (unsigned char)ba.at(i));
}
return 0;
}


0: 3c
1: b8
2: 64
3: 18
4: ca

yandi
31st January 2012, 11:39
Hi wysota..thanks

yes it's works, but can you enlighten me why in first sample it's show warning (but actually if we debug the result is ok) than your sample?.

wysota
31st January 2012, 13:10
I haven't seen the warning so I won't guess.

ChrisW67
31st January 2012, 23:25
No warnings here:


#include <QtCore>
#include <QDebug>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);

QByteArray ba;
ba.resize(5);
ba[0] = 0x3c;
ba[1] = 0xb8;
ba[2] = 0x64;
ba[3] = 0x18;
ba[4] = 0xca;
qDebug() << ba.toHex();

return 0;
}


$ make
g++ -c -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o main.o main.cpp
g++ -Wl,-O1 -Wl,-rpath,/usr/lib64/qt4 -o test main.o -L/usr/lib64/qt4 -lQtGui -L/usr/lib64 -L/usr/lib64/qt4 -L/usr/X11R6/lib -lQtCore -lgthread-2.0 -lrt -lglib-2.0 -lpthread
$ ./test
"3cb86418ca"


Perhaps tell us what environment and compiler you are using, and what the warnings actually are.

yandi
2nd February 2012, 09:58
sorry just got to forum again..

I'm in windows.. from chris example I got this ouput error :

Running build steps for project testing...
Configuration unchanged, skipping qmake step.
Starting: "D:\QtSDK\QtCreator\bin\jom.exe"
D:\QtSDK\QtCreator\bin\jom.exe -nologo -j 4 -f Makefile.Debug
cl -c -nologo -Zm200 -Zc:wchar_t- -Zi -MDd -GR -EHsc -W3 -w34100 -w34189 -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I"..\..\Desktop\Qt\4.7.4\msvc2008\include\QtCore" -I"..\..\Desktop\Qt\4.7.4\msvc2008\include" -I"..\..\Desktop\Qt\4.7.4\msvc2008\include\ActiveQt" -I"debug" -I"..\..\Desktop\Qt\4.7.4\msvc2008\mkspecs\win32-msvc2008" -Fodebug\ @C:\Users\yandikbo\AppData\Local\Temp\main.obj.507 2.0.jom
main.cpp
main.cpp(11) : warning C4309: 'argument' : truncation of constant value
main.cpp(14) : warning C4309: 'argument' : truncation of constant value
link /LIBPATH:"d:\QtSDK\Desktop\Qt\4.7.4\msvc2008\lib" /NOLOGO /DEBUG /SUBSYSTEM:CONSOLE /MANIFEST /MANIFESTFILE:"debug\testing.intermediate.manifest" /OUT:debug\testing.exe @C:\Users\yandikbo\AppData\Local\Temp\testing.exe. 5072.827.jom
mt.exe -nologo -manifest "debug\testing.intermediate.manifest" -outputresource:debug\testing.exe;1

jom 1.0.6 - empower your cores

The process "D:\QtSDK\QtCreator\bin\jom.exe" exited normally.

wysota
2nd February 2012, 10:08
Apparently your compiler treats those constants as ints. Try surrounding them in quotes. '\x68' instead of 0x68).

yandi
2nd February 2012, 10:18
aaahhh....ok
it's working now without error....thanks :)

wysota
2nd February 2012, 10:20
It was a warning, not an error. The compiler was merely informing you that the implicit conversion looses information which is ok in this case.

KillGabio
2nd February 2012, 11:08
what`s the difference between '\x68' and 0x68? one is a char* and the other one too right?

wysota
2nd February 2012, 11:30
No. First one is a char (single quotes), the other is a constant which your compiler interprets as an integer.