Results 1 to 8 of 8

Thread: Efficient/fast way convert hex to binnary

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Efficient/fast way convert hex to binnary

    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 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Efficient/fast way convert hex to binnary

    '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.

  3. The following user says thank you to SixDegrees for this useful post:

    Tadas (20th September 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Efficient/fast way convert hex to binnary

    Quote Originally Posted by SixDegrees View Post
    Offhand, I don't know if QString has padding capabilities
    Sure it does Both QString::number and QString::arg can be used.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Efficient/fast way convert hex to binnary

    You might also want to look into a bit array.

  6. #5
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Efficient/fast way convert hex to binnary

    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
    Last edited by bajarangi; 20th September 2010 at 18:55.

  7. #6
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Efficient/fast way convert hex to binnary

    Hi,
    Thank you everybody for replies.
    I'm not very good in byte programming.
    I made code, which creates string, like SixDegrees suggested.
    Qt Code:
    1. QString str("07");
    2. bool ok;
    3.  
    4. QByteArray sk, sk2("00000000");
    5. sk=QByteArray::number(str.toLongLong(&ok,16),2); //convert 07 to binary 111
    6. sk2.replace(0,sk.size(),sk); // replace first 3 values of 00000000 to 111, and result is 11100000
    7.  
    8.  
    9. qDebug()<<"sk string:" << sk[0]<< sk[1]<< sk[2]<< sk[3];
    10. qDebug()<<"sk2 string:"<< sk2[0]<< sk2[1]<< sk2[2]<< sk2[3]<< sk2[4]<< sk2[5]<< sk2[6]<< sk2[7];
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by Tadas; 20th September 2010 at 21:08.

  8. #7
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Efficient/fast way convert hex to binnary

    Here's the way I'd do it:

    Qt Code:
    1. QChar fill = '0';
    2. int num = 0x07;
    3. QString binary_str = QString("%1").arg(num,8,2,fill);
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Efficient/fast way convert hex to binnary

    Also, starting with a string you could do this:

    Qt Code:
    1. QChar fill = '0';
    2. bool ok;
    3. int num = QString("07").toInt(&ok,16);
    4. QString binary_str = QString("%1").arg(num,8,2,fill);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to make efficient apps?
    By Tomasz in forum Qt for Embedded and Mobile
    Replies: 26
    Last Post: 22nd September 2010, 23:58
  2. Take a screenshot and convert it to PNG but FAST !
    By fitzy in forum Qt Programming
    Replies: 11
    Last Post: 4th November 2009, 09:20
  3. efficient way to store menu items in file
    By h123 in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2009, 07:52
  4. Efficient way of inserting rows?
    By afflictedd2 in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2008, 21:01
  5. Efficient Scaling and Rotating of QGraphicsView
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 12th December 2006, 17:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.