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

  • Home
  • SEARCH
  • 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 181425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:45:41+00:00 2026-05-11T14:45:41+00:00

Following my earlier question . Is there a way to write a string in

  • 0

Following my earlier question. Is there a way to write a string in a compressed/bit version using C++ native idiom. I am thinking something like Perl’s native pack and unpack.

  • 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. 2026-05-11T14:45:41+00:00Added an answer on May 11, 2026 at 2:45 pm

    Based on reading your previous question, I think you mean to say that you want a binary encoded output, rather than a ‘compressed’ output. Generally, ‘compressed’ is used to refer specifically to data that has been reduced in size through the application of an algorithm such as LZW encoding. In your case, you may find that the output is ‘compressed’ in the sense that it is smaller because for a wide variety of numbers a binary representation is more efficient than an ASCII representation, but this is not ‘compression’ in the standard sense, which may be why you are having trouble getting the answer you are looking for.

    I think you are really asking the following:

    Given a number in ASCII format (stored in a std::string, for example), how can I write this to a file as a binary encoding integer?

    There are two parts to the answer. First, you must convert the ASCII encoded string to an integer value. You may use a function such as strtol, which will return a long integer equivalent in value to your ASCII encoded number. Do be aware that there are limitations on the magnitude of the number that may be represented in a long integer, so if your numbers are very, very large, you may need to be more creative in translating them.

    Second, you must write the data to the output stream using ostream::write(), which does not attempt to format the bytes you give it. If you simply use the default operator<<() stream operation to write the values, you’ll find that your numbers just get translated back to ASCII and written out that way. Put this all together like this:

    #include <stdlib.h>        // For strtol(). #include <arpa/inet.h>     // For htonl(). #include <fstream>         // For fstream. #include <string>          // For string.  int main(int argc, char *argv[]) {     char *dummy = 0;     std::string value('12345');      // Use strtol to convert to an int; '10' here means the string is      // in decimal, as opposed to, eg, hexadecimal or octol, etc.      long intValue = strtol(value.c_str(), &dummy, 10);      // Convert the value to 'network order'; not strictly necessary,      // but it is good hygiene.  Note that if you do this, you will      // have to convert back to 'host order' with ntohl() when you read      // the data back.      uint32_t netValue = htonl(intValue);      // Create an output stream; make sure to open the file in binary mode.      std::fstream output;     output.open('out.dat', std::fstream::out | std::fstream::binary);      // Write out the data using fstream::write(), not operator<<()!      output.write(reinterpret_cast<char *>(&netValue), sizeof(netValue));     output.close(); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from this question what would be the best way to write a
Following up from an earlier question on extracting the n'th regex match , I
Following on from my earlier question , I am still having issues with loading
Following my question regarding a .NET YAML Library ... as there doesn't seem to
Following this question: Good crash reporting library in c# Is there any library like
Following on from my earlier question, I have decided to have a go at
I'm following up on my earlier question: Mono.Cecil: call base class' method from other
OK, I asked this question earlier today How do I write a scope to
The following question is related to a question that I had asked earlier: Help
I asked the following question earlier and @topek answered it perfectly. However, I thought

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.