PDA

View Full Version : Efficient/fast way convert hex to binnary



Tadas
19th September 2010, 23:09
Hi,

I have hex QString value 07 I need to convert it to binary, it should be 00000111, but I can only convert it to 111 (without 0), I made loop to to check values. I will need to find out how many 1 I have and use this bits as a mask, for example 11100100
1 - do something
1 - do something
1 - do something
0 - do nothing
0 - do nothing
1 - do something
0 - do nothing
0 - do nothing

My code works, But I get many warning and I guess, there is easier and more efficient way of doing this.


QString str("07");
QByteArray gmeMask;
bool ok;
gmeMask=QByteArray::number(str.toLongLong(&ok,16),2);
qDebug()<< QByteArray::number(str.toLongLong(&ok,16),2);


for (int i=7; i>=0 ; i--)
{
if (i<gmeMask.size())
{
if(i==7||gmeMask[i]==1) qDebug()<< "gmeMask1"<<i << gmeMask[i];
if(i==6||gmeMask[i]==1) qDebug()<< "gmeMask2"<<i << gmeMask[i];
if(i==5||gmeMask[i]==1) qDebug()<< "gmeMask3"<<i << gmeMask[i];
if(i==4||gmeMask[i]==1) qDebug()<< "gmeMask4"<<i << gmeMask[i];
if(i==3||gmeMask[i]==1) qDebug()<< "gmeMask5"<<i << gmeMask[i];
if(i==2||gmeMask[i]==1) qDebug()<< "gmeMask6"<<i << gmeMask[i];
if(i==1||gmeMask[i]==1) qDebug()<< "gmeMask7"<<i << gmeMask[i];
if(i==0||gmeMask[i]==1) qDebug()<< "gmeMask8"<<i << gmeMask[i];
}
}

Results

"111"
gmeMask6 2 1
gmeMask7 1 1
gmeMask8 0 1


Warning messages

..\bitai\mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
..\bitai\mainwindow.cpp:22: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:22: note: candidate 2: operator==(int, int) <built-in>
..\bitai\mainwindow.cpp:23: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:23: note: candidate 2: operator==(int, int) <built-in>
..\bitai\mainwindow.cpp:24: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:24: note: candidate 2: operator==(int, int) <built-in>
..\bitai\mainwindow.cpp:25: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:25: note: candidate 2: operator==(int, int) <built-in>
..\bitai\mainwindow.cpp:26: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:26: note: candidate 2: operator==(int, int) <built-in>
..\bitai\mainwindow.cpp:27: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:27: note: candidate 2: operator==(int, int) <built-in>
..\bitai\mainwindow.cpp:28: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:28: note: candidate 2: operator==(int, int) <built-in>
..\bitai\mainwindow.cpp:29: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
..\bitai\mainwindow.cpp:29: note: candidate 2: operator==(int, int) <built-in>

SixDegrees
19th September 2010, 23:17
'0000111' is a string, not a number. So what you're looking for is a string representation of the binary number 111, padded on the left with zeros so the total length of the resulting string is 8.

This sort of thing is simplest to do using string manipulations. Offhand, I don't know if QString has padding capabilities, but I'd look at the documentation to check. If not, then you can simply construct an 8-charater long string of zeros, then overlay your converted string into it based on length.

wysota
19th September 2010, 23:26
Offhand, I don't know if QString has padding capabilities
Sure it does :) Both QString::number and QString::arg can be used.

tbscope
20th September 2010, 05:56
You might also want to look into a bit array.

bajarangi
20th September 2010, 17:50
I had the same problem, and searched all over for any solution, most of which would fail
in some situation or another.

This works for me for any 8 bit numeric value:

QString qchar2binstr8(const quint8 val)
{
QString binstr("");
unsigned int Mask = 0x80;
int Bit(0);
while (Mask != 0) {
binstr[Bit] = ((Mask & val) == 0) ? '0' : '1';
Mask >>=1;
++bit;
}
return binstr;

You should also look at QString::toUInt()
It should work using a Base 2 parameter.

HTH,
baj

Tadas
20th September 2010, 20:01
Hi,
Thank you everybody for replies.
I'm not very good in byte programming.
I made code, which creates string, like SixDegrees suggested.

QString str("07");
bool ok;

QByteArray sk, sk2("00000000");
sk=QByteArray::number(str.toLongLong(&ok,16),2); //convert 07 to binary 111
sk2.replace(0,sk.size(),sk); // replace first 3 values of 00000000 to 111, and result is 11100000


qDebug()<<"sk string:" << sk[0]<< sk[1]<< sk[2]<< sk[3];
qDebug()<<"sk2 string:"<< sk2[0]<< sk2[1]<< sk2[2]<< sk2[3]<< sk2[4]<< sk2[5]<< sk2[6]<< sk2[7];

Result

sk string: 1 1 1
sk2 string: 1 1 1 0 0 0 0 0

I guess it is still possible to make everything easier, but at least now, I don't get warning messages.

jonthom
18th October 2010, 20:20
Here's the way I'd do it:


QChar fill = '0';
int num = 0x07;
QString binary_str = QString("%1").arg(num,8,2,fill);

jonthom
19th October 2010, 13:54
Also, starting with a string you could do this:


QChar fill = '0';
bool ok;
int num = QString("07").toInt(&ok,16);
QString binary_str = QString("%1").arg(num,8,2,fill);