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 7997011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:51:34+00:00 2026-06-04T14:51:34+00:00

What is the correct way to convert losslessly between std::string and QByteArray… mostly for

  • 0

What is the correct way to convert losslessly between std::string and QByteArray… mostly for the purpose of handling binary data?

I’m using:

QByteArray qba = QString::fromStdString(stdString).toAscii();

and

QString(qba).toStdString();

but I wanted to check whether this is actually correct.

  • 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-04T14:51:36+00:00Added an answer on June 4, 2026 at 2:51 pm

    For binary data your solution is problematic since non-ASCII characters would be converted to '?' by QString::toAscii(). There is also the unnecessary overhead of UTF-16 conversion for the internal representation of QString. As you might have guessed, QString should only be used if the data is textual, not binary.

    Both QByteArray and std::string have constructors for raw data (C-string + length) and also a conversion to C-string + length. So you can use them for conversion:

    // std::string => QByteArray
    QByteArray byteArray(stdString.c_str(), stdString.length());
    
    // QByteArray => std::string
    std::string stdString(byteArray.constData(), byteArray.length());
    

    They are both binary-safe, meaning that the string may contain '\0' characters and doesn’t get truncated. The data also doesn’t get touched (there is no UTF conversion), so this conversion is “lossless”.

    Make sure to use the constructors with the length as the second argument (for both QByteArray and std::string), as most other constructors will truncate the data before the first occurrence of a zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the correct/best/simplest way to convert a c-style string to a std::string. The
What is the easiest and correct way to convert a String number with commas
What is the correct way to convert '\xbb' into a unicode string? I have
What is the correct way to define collection implementation using MyBatis. Consider example below.
What is the correct way to convert from mysql timestamp (YYYY-MM-DD HH:MM:SS) to my
What is the correct way to convert ASP.NET SOAP-based web services to JSON-based responses?
HI, Is there any 'correct' way to convert an XElement to an XmlNode in
What is the correct way to convert my class to/from a byte array? I
I have written a function to convert string to integer if ( data !=
Related: Correct way to document open-ended argument functions in JSDoc I've a function that

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.