Results 1 to 6 of 6

Thread: QString to Binary Problem

  1. #1

    Default QString to Binary Problem

    Hi everyone;

    I'm working on a program which user types a hexadecimal number on a text edit box and now I'm trying to convert it to a binary number.

    At last I can get the binary of that number as a QString but in the conversion to integer I have problems..


    For example when user types A00A, I can get a QString value binary_res like:


    QString binary_res="1010000000001010";
    int bin = binary_res.toInt(&ok, 10);



    but int bin returns 0.



    How can i protect the binary_res as a binary number while conversion or is there any possible ways to
    convert hexadecimal number to binary number without using QString ??


    Thanks in advance..

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QString to Binary Problem

    You tell to .toInt that your string represents a base 10 number...
    Try this:
    int bin = binary_res.toInt(&ok, );

  3. #3

    Default Re: QString to Binary Problem

    I've seen that the problem is about the max value of integer.. if it's not out of bounds, the conversion with the first code I had written works fine.. Anyway thx mvuori for your answer..

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString to Binary Problem

    the conversion with the first code I had written works fine
    No. You tell us your input string is in binary so under no circumstance does treating it as decimal during conversion give you a useful result. You are clearly very confused.

    A number is represented inside a computer as a set of binary bits; 32 in four bytes for the average int data type. The thing the user is giving you is a string that represents a number in some base; 2 in the case of binary, 10 for decimal, or 16 in the case of hexadecimal. Each character in the string is one or two bytes in the computer's memory, which clearly is a very different beast to a simple int. You can choose to convert that string to a number using the functions in QString, specifically QString::toInt(). To do that you need to tell QString what base the string is in and it will convert. If it cannot convert your result will be zero and the ok flag, if you gave it one, will be false.

    Qt Code:
    1. QString userInput("A00A"); // this is a hexadecimal string representation of a number, not a number
    2.  
    3. // So let's try to convert it
    4. bool ok;
    5. int value = userInput.toInt(&ok, 16); // input is a hexadecimal string so this should work
    6. qDebug() << ok << value; // prints: true 40970 (output is in decimal by default)
    7.  
    8. value = userInput.toInt(&ok, 10); // it is NOT a valid decimal string so this fails
    9. qDebug() << ok << value; // prints: false 0
    10. value = userInput.toInt(&ok, 2); // it is NOT a valid binary string so this fails
    11. qDebug() << ok << value; // prints: false 0
    To copy to clipboard, switch view to plain text mode 
    once you have an actual number then you can display it in any human-friendly form you like using QString::number():
    Qt Code:
    1. int value = userInput.toInt(&ok, 16);
    2. qDebug() << "The number in decimal" << QString::number(value);
    3. qDebug() << "The number in hexadecimal" << QString::number(value, 16);
    4. qDebug() << "The number in binary" << QString::number(value, 2);
    5. qDebug() << "The number in octal" << QString::number(value, 8);
    To copy to clipboard, switch view to plain text mode 
    prints:
    Qt Code:
    1. The number in decimal "40970"
    2. The number in hexadecimal "a00a"
    3. The number in binary "1010000000001010"
    4. The number in octal "120012"
    To copy to clipboard, switch view to plain text mode 
    QString::toInt() and QString::number() are in some sense opposite functions.

  5. #5
    Join Date
    Oct 2012
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QString to Binary Problem

    Quote Originally Posted by ChrisW67 View Post
    No. You tell us your input string is in binary so under no circumstance does treating it as decimal during conversion give you a useful result. You are clearly very confused.

    A number is represented inside a computer as a set of binary bits; 32 in four bytes for the average int data type. The thing the user is giving you is a string that represents a number in some base; 2 in the case of binary, 10 for decimal, or 16 in the case of hexadecimal. Each character in the string is one or two bytes in the computer's memory, which clearly is a very different beast to a simple int. You can choose to convert that string to a number using the functions in QString, specifically QString::toInt(). To do that you need to tell QString what base the string is in and it will convert. If it cannot convert your result will be zero and the ok flag, if you gave it one, will be false.

    Qt Code:
    1. QString userInput("A00A"); // this is a hexadecimal string representation of a number, not a number
    2.  
    3. // So let's try to convert it
    4. bool ok;
    5. int value = userInput.toInt(&ok, 16); // input is a hexadecimal string so this should work
    6. qDebug() << ok << value; // prints: true 40970 (output is in decimal by default)
    7.  
    8. value = userInput.toInt(&ok, 10); // it is NOT a valid decimal string so this fails
    9. qDebug() << ok << value; // prints: false 0
    10. value = userInput.toInt(&ok, 2); // it is NOT a valid binary string so this fails
    11. qDebug() << ok << value; // prints: false 0
    To copy to clipboard, switch view to plain text mode 
    once you have an actual number then you can display it in any human-friendly form you like using QString::number():
    Qt Code:
    1. int value = userInput.toInt(&ok, 16);
    2. qDebug() << "The number in decimal" << QString::number(value);
    3. qDebug() << "The number in hexadecimal" << QString::number(value, 16);
    4. qDebug() << "The number in binary" << QString::number(value, 2);
    5. qDebug() << "The number in octal" << QString::number(value, 8);
    To copy to clipboard, switch view to plain text mode 
    prints:
    Qt Code:
    1. The number in decimal "40970"
    2. The number in hexadecimal "a00a"
    3. The number in binary "1010000000001010"
    4. The number in octal "120012"
    To copy to clipboard, switch view to plain text mode 
    QString::toInt() and QString::number() are in some sense opposite functions.
    please help me, i can't convert int to octal.. i dont know why. when i push button to convert, the output is same as input

    sorry to bad english, i newbie..
    i using QString::number(value,8), to another converter, its work, but to octal, can't change anything..... pls help

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString to Binary Problem

    Please show us what you are actually doing

Similar Threads

  1. Replies: 3
    Last Post: 13th February 2012, 04:16
  2. How can I read binary data to QString?
    By zolookas in forum Newbie
    Replies: 2
    Last Post: 29th July 2008, 20:03
  3. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  4. problem running Qt4.x binary on Qt3.x
    By raman_31181 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th February 2008, 20:02
  5. Replies: 1
    Last Post: 2nd April 2007, 22:52

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.