PDA

View Full Version : QByteArray processing (seeking equivalent QT code)



pdoria
12th July 2009, 13:24
Hi,

Could someone please tell me the C++/Qt4 equivalent of this?

PHP CODE



$input_250="432345566180632659";

$input_Value=array();

//250 IN29
$dec = $input_250;
$byte = 8;
for($i=0;$i<$byte;$i++)
{
if($i>0)
{
$input_Value[$i] = gmp_mod(gmp_div_q($dec,gmp_pow(2, $i*8)),256);
}
else
{
$input_Value[$i] = gmp_mod($dec,256);
}
}


I'm storing the $input_250 in Qt in a QByteArray.
This number is represented as a longlong .

Any help would surely be appreciated! ;)

BR,
Pedro Doria Meunier

ChrisW67
13th July 2009, 01:02
If I'm reading that right then you are trying to break a 64-bit integer value into a series of bytes.

// close to the original
quint64 input250 = Q_UINT64_C(2314885536612049221);
QByteArray inputValue(sizeof(input250), 0); // bytes of zero
int i = 0;
quint64 work = input250;
while (work) { // only does as many loops as needed.
inputValue[i++] = work & 0xff;
work = work >> 8;
}
qDebug() << input250 << inputValue;

// You might get away with this but beware byte order issues
QByteArray ba = QByteArray::fromRawData((char *)&input250, sizeof(input250));
qDebug() << input250 << ba;

pdoria
13th July 2009, 09:40
Chris,

Either approach works perfectly. ;)

Many, many thanks!

BR,
Pedro Doria Meunier