PDA

View Full Version : How to convert QByteArray to char*?



Gokulnathvc
14th June 2011, 07:43
How to convert QByteArray to char*? I want to copy the contents of the file from HTTP to be read
Am using

http.read(Char* Data,len)

mcosta
14th June 2011, 08:06
QByteArray::data() or QByteArray::constData()

deyili
14th June 2011, 08:07
copy from QByteArray
use QByteArray::data() and QByteArray::count();

copy to QByteArray
use QByteArray::append

ChrisW67
14th June 2011, 08:09
Have you read the QByteArray docs? Here's a hint: Look for methods where "char *" is the return value.

Of course, "char *" is not the same as "Char *", but I have assumed that is a typo.

Gokulnathvc
14th June 2011, 10:02
But this Read function always reads the file from the beginning. I want to read certain number of characters at a time. How to do that?

wysota
14th June 2011, 10:19
But this Read function always reads the file from the beginning.
Then don't use it.

Gokulnathvc
14th June 2011, 11:42
Then don't use it.

Its not proper on your side.

wysota
14th June 2011, 12:38
What do you mean? You had said some function didn't do what you wanted so I told you to use something else that does what you want.

Gokulnathvc
14th June 2011, 12:51
I just enquired whether it if possible to read the file for particular length each time using this QHttp read method. I have tried and it is always reading from the beginning. I don't know whether its my mistake that i have used this wrongly. Help me in fixing this issue.

wysota
14th June 2011, 13:39
First of all you didn't say anything about QHttp. Second of all since the data comes from the network, we're not talking about a file but rather a stream of bytes. And as with all streams, the data is not seekable -- you need to read it from the start. If you want bytes 10-15 then read 16 bytes and discard first ten.

ChrisW67
15th June 2011, 00:38
Apart from not mentioning QHttp, which you have been told several times is obsolete and should not be used in new code, you failed to mention that you wanted to access parts of a network stream, parts of an in-memory buffer, what you had tried, why you think that didn't work, etc. I seriously don't understand how you expected your question:

How to convert QByteArray to char*?
to lead to anything resembling the answer you expected.

As wysota says, network streams are sequential. You can read an discard bits you don't want: this will work everywhere. If the upstream server supports range retrieval (http://www.lmgtfy.com/?q=http+range+retrieval) then you can request only the bytes you want: this has nothing to do with either Qt or obtaining a char * to a QByteArray's data.

ercant
23rd January 2014, 08:34
hi

it is working...

void RxParser(char *Data, int DataLen)
{
..
}

void Clients::TCPsocketready()
{
QByteArray Data = socket->readAll();
RxParser(Data.data(),Data.length());
}