conversion of binay data to qt types
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.
Re: conversion of binay data to qt types
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).
Re: conversion of binay data to qt types
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.
Re: conversion of binay data to qt types
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.
Re: conversion of binay data to qt types
I tried to read whole data usiing QDataStream >> oprator with char * ;
My code is like this ,
Code:
QFile file("binary.ked");
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?
Re: conversion of binay data to qt types
Quote:
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()
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.
Re: conversion of binay data to qt types
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.
Re: conversion of binay data to qt types
can you post your code?
what is 'i' at the time you allocate your char array? is it > 0 ?
Re: conversion of binay data to qt types
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,
Re: conversion of binay data to qt types
what I meant was does size() returns a positive value?
Try this:
Code:
QFile file("binary.ked");
{
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";
Re: conversion of binay data to qt types
ya it's returning +ve value , still same prob.
Re: conversion of binay data to qt types
did you try the code I suggested?
Re: conversion of binay data to qt types
yes , i tried ur code still its going to true part and giving the same.
Re: conversion of binay data to qt types
strange.
Try adding:
before
Code:
ds.readRawData(ch,i);
Re: conversion of binay data to qt types
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 ?
Re: conversion of binay data to qt types
Quote:
f i use readRawData i am getting some binary data
but before you said you got still the bad pointer?
What did you change?
Re: conversion of binay data to qt types
sorry ,since i need binary data as normal data i tried to use only readbytes and >> operator ,