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 add an arbitrary number of null bytes into a qbytearray upon construction the right way?
QtWS25 Last Chance

How to add an arbitrary number of null bytes into a qbytearray upon construction the right way?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 349 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.
  • D Offline
    D Offline
    devjb
    wrote on last edited by
    #1

    I am trying to construct a QByteArray which contains null bytes. Let's say two null bytes surorunded by blanks.

    So far I only managed it to do via QByteArray::fromHex("323200003232").
    Is there a way, doing it with one of the class constructors? Things like QByteArray("\x32\x32\x00\x00\x32\x32") fail, because the null bytes are obviously seen as being invalid null characters and therefore are cut out.

    JonBJ 1 Reply Last reply
    0
    • D devjb

      I am trying to construct a QByteArray which contains null bytes. Let's say two null bytes surorunded by blanks.

      So far I only managed it to do via QByteArray::fromHex("323200003232").
      Is there a way, doing it with one of the class constructors? Things like QByteArray("\x32\x32\x00\x00\x32\x32") fail, because the null bytes are obviously seen as being invalid null characters and therefore are cut out.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @devjb
      Use the overload QByteArray::QByteArray(const char *data, int size = -1) (https://doc.qt.io/qt-5/qbytearray.html#QByteArray-1) or [static]QByteArray QByteArray::fromRawData(const char *data, int size) (https://doc.qt.io/qt-5/qbytearray.html#fromRawData) and specify size (e.g. sizeof()) as the correct length of your data.

      1 Reply Last reply
      4
      • D Offline
        D Offline
        devjb
        wrote on last edited by
        #3

        Well,

        the size argument makes it work indeed :-) 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