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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:27:59+00:00 2026-05-26T21:27:59+00:00

I am trying to store two integer value into an char array in C++.

  • 0

I am trying to store two integer value into an char array in C++.
Here is the code..

char data[20];
*data = static_cast <char> (time_delay);   //time_delay is of int type
*(data + sizeof(int)) = static_cast<char> (wakeup_code);  //wakeup_code is of int type

Now on the other end of the program, I want to reverse this operation. That is, from this char array, I need to obtain the values of time_delay and wakeup_code.

How can I do that??

Thanks,
Nick

P.S: I know this is a stupid way to do this, but trust me its a constraint.

  • 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-26T21:28:00+00:00Added an answer on May 26, 2026 at 9:28 pm

    I think when you write static_cast<char>, that value is converted to a 1-byte char, so if it didn’t fit in a char to begin with, you’ll lose data.

    What I’d do is use *((int*)(data+sizeof(int))) and *((int*)(data+sizeof(int))) for both reading and writing ints to the array.

    *((int*)(data+sizeof(int))) = wakeup_code;
    ....
    wakeup_code = *((int*)(data+sizeof(int)));
    

    Alternatively, you might also write:

    reinterpret_cast<int*>(data)[0]=time_delay;
    reinterpret_cast<int*>(data)[1]=wakeup_code;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Complete sql noob here. Trying to think about a way to store some data
I am trying to store an xml value in my app.config file. The app.config
I am trying to store more than 1 data item at a single index
I'm trying to store data in the following fashion with a string as the
I am trying to store/retrieve a value that is stored in the Application Settings.
I have a class that contains two arraylists which I'm trying to store objects
I am trying to build a messaging/chat system. which can store conversation between two
I'm trying to store a number as a binary string in an array but
I'm trying to store each line from a file into an arraylist and then
C++ question this time. I'm trying to store the product between two random numbers...

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.