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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:43:42+00:00 2026-05-13T15:43:42+00:00

I have the contents of a file assigned into a string object. For simplicity

  • 0

I have the contents of a file assigned into a string object. For simplicity the file only has 5 bytes, which is the size of 1 integer plus another byte.
What I want to do is get the first four bytes of the string object and somehow store it into a valid integer variable by the program.
Then the program will do various operations on the integer, changing it.
Afterward I want the changed integer stored back into the first four bytes of the string object.

Could anyone tell me I could achieve this? I would prefer to stick with the standard C++ library exclusively for this purpose. Thanks in advance for any help.

  • 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-05-13T15:43:42+00:00Added an answer on May 13, 2026 at 3:43 pm

    The following code snippet should illustrate a handful of things. Beware of endian differences. Play around with it. Try to understand what’s going on. Add some file operations (binary read & write). The only way to really understand how to do this, is to experiment and create some tests.

    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
        int a = 108554107; // some random number for example sake
    
        char c[4];            // simulate std::string containing a binary int
        *((int *) &c[0]) = a; // use casting to copy the data
    
        // reassemble a into b, using indexed bytes from c
        int b = 0;
        b |= (c[3] & 0xff) << 24;
        b |= (c[2] & 0xff) << 16;
        b |= (c[1] & 0xff) << 8;
        b |= c[0] & 0xff;
    
        // show that all three are equivalent
        cout << "a: " << a << " b: " << b
            << " c: " << *((int *) &c[0]) << endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file. I want to get its contents into a blob column
I have a byte[] array, the contents of which represent a TIFF file (as
I have a webapp which resizes its window to exactly fit its contents: window.resizeTo(200,300)
So I have a CSV that contains a filename and a file's contents. The
I have a script which generates an XML file when the user accesses the
I have a database where each entry has a file path and a last
I have some content that i have to render as a excel file in
I have the need to copy the entire contents of a directory on a
I have an application where the contents of e-mails that get sent are stored
I have a requirement to get the contents of every method in a cs

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.