Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Problem z konwersją liczb

    Polish
    2
    3
    1638
    Loading More Posts
    • 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.
    • W
      WhyMe last edited by

      Witam.
      Mam problem z konwertowaniem liczb.

      Przykład:
      @bool ok = true;

      unsigned long long int decimal = 524288;
      QString bin;
      unsigned long long int octal = 0;
      bin.setNum(decimal, 2);
      octal = bin.toULongLong(&ok, 8);
      
      if(ok == false) std::cout << "error\n";
      
      std::cout << octal;@
      

      I wyświetla wynik: 144115188075855872, a powinien wyświetlić 2000000

      Przykład 2:
      @
      bool ok = true;

      unsigned long long int octal = 2000000;
      QString bin;
      unsigned long long int decimal = 0;
      bin.setNum(octal, 2);
      decimal = bin.toULongLong(&ok, 10);
      
      if(ok == false) std::cout << "error\n";
      
      std::cout << decimal;
      

      @

      Wyświetla mi, że błąd.

      Co takiego robię źle ??

      1 Reply Last reply Reply Quote 0
      • W
        WhyMe last edited by

        Już sobie poradziłem ^^

        @
        bool ok = true;

        QString octal("17");
        QString binary;
        QString decimal;
        QString octal2;
        binary = QString::number(octal.toULongLong(&ok, 8), 2);
        decimal.setNum(binary.toULongLong(&ok, 2), 10);
        octal2.setNum(decimal.toULongLong(&ok, 10), 8);
        if(ok == false)
        {
            ok = true;
            std::cout << "error\n";
        }
        std::cout << octal2.toStdString().c_str() << "\n\n";
        
        int ioctal;
        QString binary2;
        int idecimal;
        
        ioctal = 15;
        binary2.setNum(QString::number(ioctal, 10).toInt(&ok, 8), 2);
        idecimal = binary2.toInt(&ok, 2);
        
        if(ok == false)
        {
            ok = true;
            std::cout << "error\n";
        }
        std::cout << idecimal;
        

        @

        1 Reply Last reply Reply Quote 0
        • A
          Alek Śmierciak last edited by

          W pierwszym przykładzie do funkcji "toULongLong":http://qt-project.org/doc/qt-4.8/qstring.html#toULongLong podajesz liczbę zapisaną w systemie dwójkowym w obiekcie klasy QString, podczas gdy funkcja oczekuje liczby w systemie ósemkowym; drugi parametr do funkcji to system liczby źródłowej:
          "If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used." "QString::toULongLong":http://qt-project.org/doc/qt-4.8/qstring.html#toULongLong
          A w wyniku otrzymuje się liczbę w systemie dziesiętnym.

          W drugim przykładzie analogicznie.

          Edit: oj, spóźnione. :-)

          1 Reply Last reply Reply Quote 0
          • First post
            Last post