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 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

Ask A Question

Stats

  • Questions 242k
  • Answers 242k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There doesn't seem to be a pure java lib for… May 13, 2026 at 7:39 am
  • Editorial Team
    Editorial Team added an answer Two suggestions: Use exit to quit the bash shell and… May 13, 2026 at 7:39 am
  • Editorial Team
    Editorial Team added an answer Maybe you can set up a web service on AD… May 13, 2026 at 7:39 am

Related Questions

I have all the time zone information that is needed and it saves all
Suppose I have the following two strings containing regular expressions. How do I coalesce
To an earlier question of mine, invovling VBC and NAnt with WinForms , I
vs2008 (and earlier versions) always creates empty folders in c:\users\<username>\my documents every time it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.