PDA

View Full Version : conversion of binay data to qt types



rajeshs
3rd January 2008, 08:55
Hi All,

I am using Qt 4.3 ,

I am having binary data in my file, I am reading that data into QByteArray. I want to get some integers from that data ,

So i am using QByteArray.toInt(). But i am not getting proper int values,

How to change binary data into other types ?

please help me.

high_flyer
3rd January 2008, 09:19
for that you need to know in advance how the data is structured in the file. (serialized)
If you know that, you can just read the values directly in their right types (de-serialization).

rajeshs
3rd January 2008, 10:07
in my case file may be in any structures, i need to get whole data as String or otherwise

i need some integers that may be placed in some particular position in the file,

that may be in middle , how to handle this type of suituations in Qt?

please help me.

high_flyer
3rd January 2008, 10:17
Lets make order in things.
If you need to serialize data IN to a file, just use QDataStream and write the data to the file.
But you have to know how you did that in order to be able to read that data back.
When you read the data back, you can do it exactly as you did when you wrote it, using QDataStream - directly in the correct types, no need to convert.

You can't read binary data from a file, unless you know how it was written.
Well, you can READ it, but converting it to something understandable will be guess work.

rajeshs
3rd January 2008, 11:25
I tried to read whole data usiing QDataStream >> oprator with char * ;

My code is like this ,



QFile file("binary.ked");
file.open(QIODevice::ReadWrite);
QDataStream ds(&file);
uint i = file.size();
char *ch = new char[i];
ds>>ch;


if i tried to debug the code i am getting bad pointer for *ch ?
Is there any mistake in my code?

high_flyer
3rd January 2008, 11:36
QDataStream & QDataStream::operator>> ( char *& s )

This is an overloaded member function, provided for convenience.

Reads the '\0'-terminated string s from the stream and returns a reference to the stream.

Space for the string is allocated using new -- the caller must destroy it with delete[].
If the data in the file is not a '\0' string.
Probably you don't have a '\0' in the data, resulting in a NULL pointer.
Try using QDataStream::readRawData() (http://doc.trolltech.com/4.3/qdatastream.html#readRawData)

note the last line in the docs!


But what it is you are trying to do?
Do you know how the file was written?
If so, you can just de-serialize it.

rajeshs
3rd January 2008, 11:46
Sorry, I tried even readRawData instead readBytes, I am getting same BadPtr.


even i tried to add \0 also at the end of the file , i am gettiing same BadPtr.

high_flyer
3rd January 2008, 12:00
can you post your code?
what is 'i' at the time you allocate your char array? is it > 0 ?

rajeshs
3rd January 2008, 12:12
Whatever code i posted previously is the correct code,

I am giving down ,

i is size() of file,

QFile file("binary.ked");
file.open(QIODevice::ReadWrite);
QDataStream ds(&file);
uint i = file.size();
char *ch = new char[i]; ds>>ch;

I tried even some other values like ( 1,2,3,4,5) for i , its giving same o/p ?

please help me,

high_flyer
3rd January 2008, 12:18
what I meant was does size() returns a positive value?
Try this:

QFile file("binary.ked");
if(file.open(QIODevice::ReadWrite))
{
QDataStream ds(&file);
uint i = file.size();
if(i>0)
{
char *ch = new char[i];
ds.readRawData(ch,i);
}
else qDebug()<<"size is null";
}
else qDebug()<<"could not open file";

rajeshs
3rd January 2008, 12:25
ya it's returning +ve value , still same prob.

high_flyer
3rd January 2008, 12:26
did you try the code I suggested?

rajeshs
3rd January 2008, 14:10
yes , i tried ur code still its going to true part and giving the same.

high_flyer
3rd January 2008, 14:20
strange.
Try adding:

if(ch != NULL)
before

ds.readRawData(ch,i);

rajeshs
4th January 2008, 03:47
if i use readRawData i am getting some binary data , but if i use readBytes i am getting bad pointer , i need to get string data in ordinary format not in binary format , how to achieve this ?

high_flyer
4th January 2008, 09:28
f i use readRawData i am getting some binary data
but before you said you got still the bad pointer?
What did you change?

rajeshs
4th January 2008, 12:26
sorry ,since i need binary data as normal data i tried to use only readbytes and >> operator ,