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 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 file contents in a java string variable, which I want to convert
So I have the following .htaccess file contents from my other project which is
I have the following contents in my XML file: <items> <item id=1><content><![CDATA[<p>string</p>]]></content></item> </items> I
I have created a UIView object using the contents of a NIB file like
I have a file contents of which looks like this 123,1,ABC,DEF 123,1,ABC 345,4,TZY 456,3,XYZ
I have a byte[] with the contents of file. I would like to send
I have a file with contents abc def high lmn ... ... There are
I have a bash script that sources contents from another file. The contents of
I have a database column that contains the contents of a file. I'm converting
I have a Tkinter textbox set to display the contents of a file. An

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.