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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:47:01+00:00 2026-06-11T17:47:01+00:00

I need to communicate with an 8 bit device on which there is no

  • 0

I need to communicate with an 8 bit device on which there is no nthol function. It is also impossible to use protocol buffers library.

Will following C function produce the same output string no matter on what architecture it is run (different endianness, different number of bits):

void encode(uint32_t input, unsigned char* buf, int buf_len) {
  assert(sizeof(uint32_t) <= buf_len);
  buf[0] = input >> 24 & 0xFF;
  buf[1] = input >> 16 & 0xFF;
  buf[2] = input >> 8 & 0xFF;
  buf[3] = input & 0xFF;
}

Will following function correctly read such encoded string and produce the same result on all architectures:

void decode(const unsigned char* buf, int buf_len, uint32_t* output) {
  assert(sizeof(uint32_t) <= buf_len);
  *output = buf[0];
  *output <<= 8;
  *output |= buf[1];
  *output <<= 8;
  *output |= buf[2];
  *output <<= 8;
  *output |= buf[3];
}

Is it possible that on an 8 bit processor 32 bit integers won’t be handled correctly for some reason?

  • 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-06-11T17:47:03+00:00Added an answer on June 11, 2026 at 5:47 pm

    Yes, that looks fine. A couple of comments:

    1. You should use uint8_t instead of assuming unsigned char is 8-bit, when the code relies on that fact.
    2. It would be a bit cleaner (imo) to do the receive using a local variable, so you don’t have to shift through the pointer to output all the time.
    3. You can probably just return the decoded value, making the decode() function a bit cleaner.

    –

     uint32_t decode(const uint8_t *buf, size_t buf_len) {
       uint32_t out = 0;
       assert(sizeof out <= buf_len);
       out |= buf[0];
       out <<= 8;
       out |= buf[1];
       out <<= 8;
       out |= buf[2];
       out <<= 8;
       out |= buf[3];
       return out;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a device emulator which can communicate over real IP address
I need to connect the Symbian OS device and communicate with files. Is there
I write app for Android which will be communicate with HTTP server. I need
I need to communicate with a hardware device using TCP and with Windows I
I need to communicate with a RS232 device, I have no specs or information
I need to develop an ANDROID client that will communicate with the server that
I need to build a live preview in Flash which will show a user
I need to write a program that will communicate with other .NET programs ...
I'm working on a project where I need to communicate with a device via
I have a Dataman high speed ID scanner which I need to communicate with

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.