[solved] Problems passing QByteArray as a pointer
-
Hi everyone, I have quite a simple problem, but I cant find the solution for it, so ill try to explain it the best I can.
I need to create a function that would shift a whole QByteArray 1 bit, either to the left, or to the right. For example, if i had (0x00,0x80,0x44) and i shift it LEFT, i would like to have (0x01,0x00,0x88). The bits that are pushed "out" of the Array, are lost. And the new ones that i get, are set to 0.Ive created a method that works perfectly, and its like this:
@
QByteArray MyProyect::Shift(QByteArray Vector, unsigned char Side)
{
int length,jj;
unsigned char aux,left=0,right=0;
length=Vector.size();
if(Side==RIGHT)
{
left=0;
right=0;
for(int i=0;i<length;i++)
{
aux=vector[i];
right=aux&(0x01);
aux=aux>>1;
aux|=left;
left=right<<7;
vector[i]=aux;
}
}
else if (Side==LEFT)
{
left=0;
right=0;
for(int j=0;j<length;j++)
{
jj=length-(j+1);
aux=vector[jj];
left=aux&(0x80);
aux=aux<<1;
aux|=right;
right=left>>7;
vector[jj]=aux;
}} return vector;
}
@I know this method isnt exactly "tidy" (specially the Left side, that instead of doing a reverse loop i changed i for jj), but it works, perfectly.
My problem is that, in my application, the QByteArray that needs to be shifted is extremely big, so, in order to save some time, id like to use a pointer, because otherwise, a copy needs to be done, and that takes a lot of time, that isnt necessarySo, to begin with I change
@
QByteArray MyProyect::Shift(QByteArray Vector, unsigned char Side)
@for
@
void MyProyect::Shift(QByteArray *Vector, unsigned char Side)
@Then, some minor modifications need to be done, and i cant do that. For example the line
@
aux=vector[i];
@is changed for
@
aux=vector->at(i);
@I havent got any problem reading the pointer of the QByteArray, to do that, im using the "at" method.
What i havent been able to do is saving into the QByteArray. To be more specific, the line@
vector[i]=aux;
@Ive tried the following
@
*(vector[i])=aux;
*(vector)[i]=aux;
vector->replace(i,1,(unsigned char)aux);
vector->replace(i,1,(const char)aux);
vector->replace(i,1,QString(%1).arg(aux));
vector->replace(i,1,QString(%1).arg(aux),1);
@none of them seem to work. I know that this is a basic concept, but ive ran out of ideas. I have a pointer to a QByteArray, and i would like to have the object itself. If i do something like
@
QByteArray NewArray=*vector;
@and then work with NewArray, it works fine, but im copying vector, and thats what im trying to avoid. Any ideas please?
To sum up, i have a pointer to a QByteArray, and i would like to work it the same way I do if i have the object, (or at least be able to save bytes in it). I cant make a copy of it (the QByteArray is of 15Mb aprox).
Thanks in advance!
-
Hey,
why don't you use a reference and leave the rest of the code as it was before you changed it?
@QByteArray MyProyect::Shift(QByteArray &Vector, unsigned char Side)
@bb
-
Because im extremelly stupid, and hadnt thought of that.
Thanks! it works perfectly!
:D