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. Appending a single byte to a QByteArray
Forum Updated to NodeBB v4.3 + New Features

Appending a single byte to a QByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 4.2k Views 2 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.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on last edited by
    #1

    I'm trying to append the byte 0x89 (10001001) to a QByteArray as follows:

    QByteArray bytes;
    bytes.append(0x89);
    

    However, this gives me the following warning:

    warning C4309: 'argument': truncation of constant value
    

    I understand the reason for this, namely that 0x89 is of type int, while QByteArray::append() expects a parameter of type char, so the 0x89 int value is truncated to char, which is dangerous.

    I am now wondering what is the clean way to solve this warning. The following works and does not give me a warning:

    bytes.append(char(0x89));
    

    but I don't like that solution because it uses old-style C-casts. I therefore tried:

    bytes.append(static_cast<char>(0x89));
    

    but for that variant, I also get the warning

    warning C4309: 'static_cast': truncation of constant value
    

    I also tried

    bytes.append(QByteArray::number(0x89));
    

    but that variant doesn't seem to insert the correct byte in my bytearray. If I print it using

    qDebug() << bytes.toHex();
    

    I get "313337" instead of "89".

    So what is the correct, most elegant and portable way to put 0x89 (10001001) into my QByteArray and avoid the warning?

    kshegunovK 1 Reply Last reply
    0
    • B Bart_Vandewoestyne

      I'm trying to append the byte 0x89 (10001001) to a QByteArray as follows:

      QByteArray bytes;
      bytes.append(0x89);
      

      However, this gives me the following warning:

      warning C4309: 'argument': truncation of constant value
      

      I understand the reason for this, namely that 0x89 is of type int, while QByteArray::append() expects a parameter of type char, so the 0x89 int value is truncated to char, which is dangerous.

      I am now wondering what is the clean way to solve this warning. The following works and does not give me a warning:

      bytes.append(char(0x89));
      

      but I don't like that solution because it uses old-style C-casts. I therefore tried:

      bytes.append(static_cast<char>(0x89));
      

      but for that variant, I also get the warning

      warning C4309: 'static_cast': truncation of constant value
      

      I also tried

      bytes.append(QByteArray::number(0x89));
      

      but that variant doesn't seem to insert the correct byte in my bytearray. If I print it using

      qDebug() << bytes.toHex();
      

      I get "313337" instead of "89".

      So what is the correct, most elegant and portable way to put 0x89 (10001001) into my QByteArray and avoid the warning?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @Bart_Vandewoestyne said in Appending a single byte to a QByteArray:

      bytes.append('\x89');
      

      should serve you fine.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3
      • B Offline
        B Offline
        Bart_Vandewoestyne
        wrote on last edited by
        #3

        Thanks! While you were typing that, I also figured that out myself :-)
        See also http://en.cppreference.com/w/c/language/character_constant
        Thanks!

        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