Originally Posted by
beemaneni
So when i put my first value(-80) in to file it shud be stored with 4 bytes as FFFFFFFB.
The int value -80 should be FFFFFFB0 in hex (I assume FFFFFFFB was a typo). I ran the small program below to show what is actually written when you use QDataStream to output the data to a file. I output the -80 integer value and a string, encoded to UTF8 as seen below:
testf.
open(QFile::WriteOnly);
int test_int = -80;
QString test_string
= "This has been a test.";
stream << test_int << test_string.toUtf8();
testf.close();
QFile testf("test.dat");
testf.open(QFile::WriteOnly);
int test_int = -80;
QString test_string = "This has been a test.";
QDataStream stream(&testf);
stream << test_int << test_string.toUtf8();
testf.close();
To copy to clipboard, switch view to plain text mode
This results in the following contents in test.dat, which will show you that by simply using QDataStream to output the data, no magic is needed. The string value is preceeded by the length of the string, i.e. hex 00000015 is 21, the length of the string I wrote out. If you use QDataStream to output your data, you can also use QDataStream to read the values back from the file, as long as you use the same data type and order, etc.
Here's a screen shot of what the contents of test.dat looks like:
hex.jpg
So, just read the data from your database into local variables and then output using QDataStream, no magic required for converting to hex.
Edit: If you don't want to use local variables as in your example, then just do the following:
stream << query.value(1).toInt(); // just for all fields i have put to int which takes 4 bytes..
stream << query.value(2).toInt(); // result of this needs to be 1 byte which is tiny int.
stream << query.value(3).toInt();
stream << query.value(4).toInt();
stream << query.value(5).toString();
stream << query.value(1).toInt(); // just for all fields i have put to int which takes 4 bytes..
stream << query.value(2).toInt(); // result of this needs to be 1 byte which is tiny int.
stream << query.value(3).toInt();
stream << query.value(4).toInt();
stream << query.value(5).toString();
To copy to clipboard, switch view to plain text mode
For query.value(2), QVariant doesn't seem to have a single byte integer type conversion available, but it will work fine to simply output a 4 byte integer for query.value(2) as well.
Bookmarks