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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:29:40+00:00 2026-05-10T14:29:40+00:00

I have a protocol that requires a length field up to 32-bits, and it

  • 0

I have a protocol that requires a length field up to 32-bits, and it must be generated at runtime to describe how many bytes are in a given packet.

The code below is kind of ugly but I am wondering if this can be refactored to be slightly more efficient or easily understandable. The problem is that the code will only generate enough bytes to describe the length of the packet, so less than 255 bytes = 1 byte of length, less than 65535 = 2 bytes of length etc…

{     extern char byte_stream[];     int bytes = offset_in_packet;     int n = length_of_packet;    /* Under 4 billion, so this can be represented in 32 bits. */     int t;    /* 32-bit number used for temporary storage. */      /* These are the bytes we will break up n into. */     unsigned char first, second, third, fourth;      t = n & 0xFF000000;     /* We have used AND to 'mask out' the first byte of the number. */     /* The only bits which can be on in t are the first 8 bits. */     first = t >> 24;     if (t)  {         printf('byte 1: 0x%02x\n',first );         byte_stream[bytes] = first; bytes++;         write_zeros = 1;     }     /* Now we shift t so that it is between 0 and 255. This is the first, highest byte of n. */     t = n & 0x00FF0000;     second = t >> 16;     if (t || write_zeros) {         printf('byte 2: 0x%02x\n', second );         byte_stream[bytes] = second; bytes++;         write_zeros = 1;     }      t = n & 0x0000FF00;     third = t >> 8;     if ( t || write_zeros) {         printf('byte 3: 0x%02x\n', third );         byte_stream[bytes] = third; bytes++;         write_zeros = 1;     }      t = n & 0x000000FF;     fourth = t;     if (t || write_zeros) {         printf('byte 4: 0x%02x\n', fourth);         byte_stream[bytes] = fourth; bytes++;     } } 
  • 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-10T14:29:40+00:00Added an answer on May 10, 2026 at 2:29 pm

    Really you’re only doing four calculations, so readability seems way more important here than efficiency. My approach to make something like this more readable is to

    1. Extract common code to a function
    2. Put similar calculations together to make the patterns more obvious
    3. Get rid of the intermediate variable print_zeroes and be explicit about the cases in which you output bytes even if they’re zero (i.e. the preceding byte was non-zero)

    I’ve changed the random code block into a function and changed a few variables (underscores are giving me trouble in the markdown preview screen). I’ve also assumed that bytes is being passed in, and that whoever is passing it in will pass us a pointer so we can modify it.

    Here’s the code:

    /* append byte b to stream, increment index */ /* really needs to check length of stream before appending */ void output( int i, unsigned char b, char stream[], int *index ) {     printf('byte %d: 0x%02x\n', i, b);     stream[(*index)++] = b; }   void answer( char bytestream[], unsigned int *bytes, unsigned int n) {     /* mask out four bytes from word n */     first  = (n & 0xFF000000) >> 24;     second = (n & 0x00FF0000) >> 16;     third  = (n & 0x0000FF00) >>  8;     fourth = (n & 0x000000FF) >>  0;      /* conditionally output each byte starting with the */     /* first non-zero byte */     if (first)         output( 1, first, bytestream, bytes);      if (first || second)         output( 2, second, bytestream, bytes);      if (first || second || third)         output( 3, third, bytestream, bytes);      if (first || second || third || fourth)         output( 4, fourth, bytestream, bytes);  } 

    Ever so slightly more efficient, and maybe easier to understand would be this modification to the last four if statements:

        if (n>0x00FFFFFF)         output( 1, first, bytestream, bytes);      if (n>0x0000FFFF)         output( 2, second, bytestream, bytes);      if (n>0x000000FF)          output( 3, third, bytestream, bytes);      if (1)         output( 4, fourth, bytestream, bytes); 

    I agree, however, that compressing this field makes the receiving state machine overly complicated. But if you can’t change the protocol, this code is much easier to read.

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

Sidebar

Ask A Question

Stats

  • Questions 71k
  • Answers 72k
  • 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
  • added an answer It seems like you can in fact send S/MIME encrypted… May 11, 2026 at 1:30 pm
  • added an answer This is a very good question, and one not limited… May 11, 2026 at 1:29 pm
  • added an answer When you open a file for reading in .NET it… May 11, 2026 at 1:29 pm

Related Questions

No related questions found

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.