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 to convert a ByteArray.at() to an int

How to convert a ByteArray.at() to an int

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 19.3k Views
  • 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.
  • S Offline
    S Offline
    shalongbasi
    wrote on 6 Mar 2013, 01:57 last edited by
    #1

    @
    void aa(QbyteArray data)
    { QChar year;
    QString str;
    year=data.at(2);
    str=QString("%1").arg(year.digitValue()+100,4,10,'0');
    }
    @

    got warning "ISO C++ says that these are ambiguous, even though the worst conversion for the first is better tahn the worst conversion for the second
    candidate 1:QString QString::arg(double,int, char,int, const Qchar &) const
    candidate 2:QString QString::arg(short int, int, int, const Qchar &) const

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wakeuky
      wrote on 6 Mar 2013, 02:46 last edited by
      #2

      Take this example:

      @QByteArray x;
      x.resize(2);
      x[0] = 0x01;
      x[1] = 0x2A;

      int num = 0;
      num |= x[0];
      num << 8;
      num |= x[1];
      num << 8;
      qDebug() << "num = " << num;@
      

      Output:
      @num = 43 @

      Example grabbed from: "This Forum Post":http://www.qtforum.org/article/15603/converting-qbytearray-elements-into-integer.html#post63766

      I believe you could simply say:

      @void aa(QByteArray data)
      {
      //QChar year;
      int year = 0;
      year |= data[2];
      QString str;
      str=QString("%1").arg(year+100,4,10,'0');
      }@

      Don't Panic

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shalongbasi
        wrote on 6 Mar 2013, 16:08 last edited by
        #3

        thanks, but that does not solve the problem. after searching, adding QChar to '0' makes warning gone.

        str=QString("%1").arg(year+100,4,10,QChar('0'));

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BelenMuñoz
          wrote on 7 Mar 2013, 07:35 last edited by
          #4

          Maybe using QString can help, QString has a toIntro method.
          Something like this (I haven't tried, but it's just an idea):

          @QString numberAux = QByteArray.at(0);
          int number;
          bool ok = true;
          number = numberAux.toInt(&ok);
          if(!ok)
          // numberAux wasn't an integer
          return error;@

          Me casé con un enano pa jartarme de reí.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 7 Mar 2013, 08:11 last edited by
            #5

            QByteArray::at() returns a char, which in ISO C++ is defined as a bit shorter than 2 bytes (0-127 instead of 255). Compilers complain that the conversion is ambiguous because of that. In most cases, warning can be ignored or a simple (int) conversion will do.

            (Z(:^

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on 7 Mar 2013, 08:32 last edited by
              #6

              @int n = int(array.at(x));@

              or type conversion can be omitted:

              @int n = array.at(x);@

              it should return int in range -127 ... +128

              :)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 7 Mar 2013, 08:34 last edited by
                #7

                @
                int n = (int) array.at(x);
                @

                Your solution may work, too.

                Edit: yeah, it should all be automatic. I think the explicit conversion does get rid of the warning, though. I might not remember this well, to be honest ;)

                (Z(:^

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  utcenter
                  wrote on 7 Mar 2013, 08:39 last edited by
                  #8

                  [quote author="sierdzio" date="1362643883"]QByteArray::at() returns a char, which in ISO C++ is defined as a bit shorter than 2 bytes (0-127 instead of 255)[/quote]

                  Umm... what? A bit shorter than 2 bytes is 15 bits, which can encode 32768 values. In every C++ implementation known to me a char is 1 byte which is 8 bits.

                  According to the C++ standard ISO:

                  bq. Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types. In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.

                  @shalongbasi - ignoring your example function that does absolutely nothing to an object outside of its scope, just cast your char to an int, which is completely safe since no information is lost, and this will resolve the ambiguity.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sierdzio
                    Moderators
                    wrote on 7 Mar 2013, 08:42 last edited by
                    #9

                    Doh, of course, utcenter. Sorry for not applying enough brain power to answering this ;)

                    (Z(:^

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      utcenter
                      wrote on 7 Mar 2013, 09:00 last edited by
                      #10

                      BTW, the '0' final argument might very well be the source of ambiguity here. Don't know if the QChar constructor is explicit or there is just too much stuff that converts from a char, but replacing '0' with QChar('0') should solve it.

                      1 Reply Last reply
                      0

                      7/10

                      7 Mar 2013, 08:34

                      • Login

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