Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:31:47+00:00 2026-06-15T11:31:47+00:00

It takes 4 bytes to represent an integer. How can I store an int

  • 0

It takes 4 bytes to represent an integer. How can I store an int in a QByteArray so that it only takes 4 bytes?

  • QByteArray::number(..) converts the integer to string thus taking up more than 4 bytes.
  • QByteArray((const char*)&myInteger,sizeof(int)) also doesn’t seem to work.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:31:48+00:00Added an answer on June 15, 2026 at 11:31 am

    There are several ways to place an integer into a QByteArray, but the following is usually the cleanest:

    QByteArray byteArray;
    QDataStream stream(&byteArray, QIODevice::WriteOnly);
    
    stream << myInteger;
    

    This has the advantage of allowing you to write several integers (or other data types) to the byte array fairly conveniently. It also allows you to set the endianness of the data using QDataStream::setByteOrder.

    Update

    While the solution above will work, the method used by QDataStream to store integers can change in future versions of Qt. The simplest way to ensure that it always works is to explicitly set the version of the data format used by QDataStream:

    QDataStream stream(&byteArray, QIODevice::WriteOnly);
    stream.setVersion(QDataStream::Qt_5_10); // Or use earlier version
    

    Alternately, you can avoid using QDataStream altogether and use a QBuffer:

    #include <QBuffer>
    #include <QByteArray>
    #include <QtEndian>
    
    ...
    
    QByteArray byteArray;
    QBuffer buffer(&byteArray);
    buffer.open(QIODevice::WriteOnly);
    myInteger = qToBigEndian(myInteger); // Or qToLittleEndian, if necessary.
    buffer.write((char*)&myInteger, sizeof(qint32));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is long long type ? It's an integer which takes 8 bytes ,
I need to parse the bytes from a file so that I only take
Lets assume i have a function that takes 32bit integer in, and returns random
I have a list of bytes that represent raw samples read in from an
How can I convert two unsigned integers that represent the digit and decimal part
What is the size of each asm instruction? Every instruction takes how many bytes?
I have a web service that takes a byte[] and saves it. This works
In Java, I can't take a byte array of unsigned bytes (from something such
I have a chunk of bytes taken from a recording file that represents a
i need to know how can i resolve Unions and Type** (e.g. int**) with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.