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.

Qt Code:
  1. QString str("07");
  2. QByteArray gmeMask;
  3. bool ok;
  4. gmeMask=QByteArray::number(str.toLongLong(&ok,16),2);
  5. qDebug()<< QByteArray::number(str.toLongLong(&ok,16),2);
  6.  
  7.  
  8. for (int i=7; i>=0 ; i--)
  9. {
  10. if (i<gmeMask.size())
  11. {
  12. if(i==7||gmeMask[i]==1) qDebug()<< "gmeMask1"<<i << gmeMask[i];
  13. if(i==6||gmeMask[i]==1) qDebug()<< "gmeMask2"<<i << gmeMask[i];
  14. if(i==5||gmeMask[i]==1) qDebug()<< "gmeMask3"<<i << gmeMask[i];
  15. if(i==4||gmeMask[i]==1) qDebug()<< "gmeMask4"<<i << gmeMask[i];
  16. if(i==3||gmeMask[i]==1) qDebug()<< "gmeMask5"<<i << gmeMask[i];
  17. if(i==2||gmeMask[i]==1) qDebug()<< "gmeMask6"<<i << gmeMask[i];
  18. if(i==1||gmeMask[i]==1) qDebug()<< "gmeMask7"<<i << gmeMask[i];
  19. if(i==0||gmeMask[i]==1) qDebug()<< "gmeMask8"<<i << gmeMask[i];
  20. }
  21. }
To copy to clipboard, switch view to plain text mode 

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

Warning messages
Qt Code:
  1. ..\bitai\mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
  2. ..\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:
  3. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  4. ..\bitai\mainwindow.cpp:22: note: candidate 2: operator==(int, int) <built-in>
  5. ..\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:
  6. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  7. ..\bitai\mainwindow.cpp:23: note: candidate 2: operator==(int, int) <built-in>
  8. ..\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:
  9. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  10. ..\bitai\mainwindow.cpp:24: note: candidate 2: operator==(int, int) <built-in>
  11. ..\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:
  12. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  13. ..\bitai\mainwindow.cpp:25: note: candidate 2: operator==(int, int) <built-in>
  14. ..\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:
  15. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  16. ..\bitai\mainwindow.cpp:26: note: candidate 2: operator==(int, int) <built-in>
  17. ..\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:
  18. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  19. ..\bitai\mainwindow.cpp:27: note: candidate 2: operator==(int, int) <built-in>
  20. ..\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:
  21. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  22. ..\bitai\mainwindow.cpp:28: note: candidate 2: operator==(int, int) <built-in>
  23. ..\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:
  24. c:\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qbytearray.h:456: note: candidate 1: bool QByteRef::operator==(char) const
  25. ..\bitai\mainwindow.cpp:29: note: candidate 2: operator==(int, int) <built-in>
To copy to clipboard, switch view to plain text mode