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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:29:31+00:00 2026-05-12T20:29:31+00:00

Please note that this is not homework and i did search before starting this

  • 0

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array?

I was looking for an answer but didn’t get any satisfactory answer in the above thread.

Here’s my requirement: I want to encode my data(say an integer) in a byte array and then transfer over the network and then decode at the other end and process it.

Here’s the encoding part:

const int MAX=5;
uint32_t a = 0xff00ffaa;
char byte_array[1024]; // this is the array to be transferred over the network
char buff[MAX]="";
sprintf(buff,"%4d",a);
memcpy(byte_array,buff,4);
// fill remaining stuff in the byte array and send it over the network

Here’s the decoding part:

const int MAX=5;
char buff[MAX]="";
strncat(buff,byte_array,4)

int i=atoi(buff);
// Work with i

Here are my questions :

1) Is the above code portable? I guess it is( please correct me)

2) Now, I wish to encode the byte array with 3 bytes (but the integer size is 4) i.e say the integer stores 0x00ffaabb and i just want the byte array to have ff int 0th index aa in the 1st index and bb in the 2nd index. How to do that?

snprinf doesn’t seem to work or may be i am missing something.

A person who has implemented any network protocol can easily help me out.
Decoding logic would still work i guess. (strncat(buff,byte_array,3) followed by atoi function call).

Here’s what the protocol says :

    --------+--------+--------+--------+------------------------------
    |Version|   3       byte    length |  Remaining stuff
    --------+--------+--------+--------+------------------------------

Version is 1 byte, followed by 3 byte length of the message.

I hope I could clarify my problem

  • 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-12T20:29:31+00:00Added an answer on May 12, 2026 at 8:29 pm

    You’re storing as ASCII, where you should be storing the bytes themselves.

    The encoding should be something like:

    uint32_t a = 0xff00ffaa;
    unsigned char byte_array[1024];
    

    Notice how I made your target array unsigned, to indicate that it’s “raw bytes”, and not actually characters.

    byte_array[0] = a >> 24;
    byte_array[1] = a >> 16;
    byte_array[2] = a >> 8;
    byte_array[3] = a >> 0;
    

    This serializes the variable a into the four first bytes of byte_array using big-endian byte ordering, which is sort of the default for many network protocols.

    You may also want to see my answer here: question 1577161.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note that this question is not about the mysql query per se, but
Please note that this question is not about the mysql query per se, but
EDIT: Please note that this question is OUTDATED; RVM got way easier to use
Please note that this question is about CGLayer (which you typically use to draw
the title pretty much explains it. Please note that this is a WebKit-specific issue
Please note - I am not looking for the right way to open/read a
i am doing this: MsgBox ActiveWorkbook.Name and getting the above mentioned error. please note
Please note the Edit below for a lot more information, and a possible solution
Please note: In each step I describe below I'm logged in as the same
How do I print debug messages in the Google Chrome JavaScript Console? Please note

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.