Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Polish
  4. Problem z konwersją liczb
Forum Updated to NodeBB v4.3 + New Features

Problem z konwersją liczb

Scheduled Pinned Locked Moved Polish
3 Posts 2 Posters 2.2k 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.
  • W Offline
    W Offline
    WhyMe
    wrote on last edited by
    #1

    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
    0
    • W Offline
      W Offline
      WhyMe
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        Alek Śmierciak
        wrote on last edited by
        #3

        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
        0

        • Login

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