Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i convert hexadecimal to binary in qt?
Forum Updated to NodeBB v4.3 + New Features

How can i convert hexadecimal to binary in qt?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 18.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurora
    wrote on 6 Mar 2012, 06:28 last edited by
    #1

    Hi,
    How can i convert a hexadecimal to binary in qt?
    I am trying as below but its not giving correct binary value....it returns 0 for any hexa value entered...

    I am reading hexadecimal as a string(actually this number resides inside a file, hence reading as a string)
    then converting this string into hexadecimal,
    then converting this to binary...
    the code as shown below...

    this function takes hexadecimal value as string then returns the binary corresponding value

    @ int FilterColl::BinVal(QString str)
    {
    bool ok;

    int iVal=str.toInt(&ok,16);
    if(ok)
    {
    QString hexnum=QString::number(iVal,16);
    std::cout<<"THE HEX VALUE IS:"<<hexnum.toStdString()<<endl;
    bool ok1;
    iVal=hexnum.toInt(&ok1,2);
    if(ok1)
    {
    cout<<"THE BINARY VALUES IS:"<<iVal<<endl;
    return iVal;
    }
    }
    else
    {
    cout<<"CONVERSION FAILED !!!!!!!!!!!!!"<<endl;
    iVal=0;
    return (iVal);
    }
    }@
    
    1 Reply Last reply
    0
    • N Offline
      N Offline
      nish
      wrote on 6 Mar 2012, 06:56 last edited by
      #2

      hexnum.toInt(&ok1,2); will always fail because the number is in hex (for eg. "0xAB11"),

      since you are returning an int in the function.. it does not matter how it is converted from string.
      just return the number in hex.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on 6 Mar 2012, 07:03 last edited by
        #3

        QString::to...() converts from a given base, QString::number() converts to a given base, thus <code>iVal = hexnum.toInt(&ok1, 2)</code> makes no sense, as you are trying to convert a hexadecimal number using base 2. In addition, your method has no return path for <code>ok1</code> beeing false.

        @
        QString hexadecimalNumber = "0xDEADBEEF";

        bool ok = false;
        QString binaryNumber = QString::number(hexadecimalNumber.toLongLong(&ok, 16), 2);

        qDebug() << binaryNumber; // "11011110101011011011111011101111"
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aurora
          wrote on 6 Mar 2012, 07:47 last edited by
          #4

          Thank u Lukas...
          And is there any standard function in Qt to perform AND operation on two strings of digit?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            foxyz
            wrote on 6 Mar 2012, 08:28 last edited by
            #5

            Thumb Lucas, In my mind, There is no direct method to perform "And" operation on two string-digit

            I just know coding and coding

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on 6 Mar 2012, 08:41 last edited by
              #6

              [quote author="aurora" date="1331020035"]Thank u Lukas...
              And is there any standard function in Qt to perform AND operation on two strings of digit? [/quote]

              I don't think so. <code>+</code> always returns a string which is the result of concatenating both strings. If you want to add them you will have to convert it to some numeric datatype, perform the addition and convert it back to a string.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Drico
                wrote on 25 Jan 2013, 21:03 last edited by
                #7

                Always good to know how to do this stuff manually. So here is a link.
                "How to convert hex to decimal manually and back":http://easyguyevo.hubpages.com/hub/Convert-Hex-to-Decimal

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  teknologya
                  Banned
                  wrote on 8 Jan 2020, 06:30 last edited by teknologya 1 Aug 2020, 06:30
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved