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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:30:20+00:00 2026-05-25T12:30:20+00:00

I need to be able to be able to send a numeric value to

  • 0

I need to be able to be able to send a numeric value to a remote socket server and so I need to encode possible numbers as bytes.

The numbers are up to 64 bit, ie requiring up to 8 bytes. The very first byte is the type, and it is always a number under 255 so fits in 1 byte.

For example, if the number was 8 and the type was a 32 bit unsigned integer then the type would be 7 which would be copied to the first (leftmost) byte and then the next 4 bytes would be encoded with the actual number (8 in this case).

So in terms of bytes:

byte1: 7
byte2: 0
byte3: 0
byte4: 0
byte5: 8

I hope this is making sense.

Does this code to perform this encoding look like a reasonable approach?

int type = 7;
uint32_t number = 8;

unsigned char* msg7 = (unsigned char*)malloc(5);
unsigned char* p = msg7;

*p++ = type;

 for (int i = sizeof(uint32_t) - 1; i >= 0; --i) 
    *p++ = number & 0xFF << (i * 8);  
  • 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-25T12:30:21+00:00Added an answer on May 25, 2026 at 12:30 pm

    You’ll want to explicitly cast type to avoid a warning:

    *p++ = (unsigned char) type;
    

    You want to encode the number with most significant byte first, but you’re shifting in the wrong direction. The loop should be:

    for (int i = sizeof(uint32_t) - 1; i >= 0; --i)
        *p++ = (unsigned char) ((number >> (i * 8)) & 0xFF);
    

    It looks good otherwise.

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

Sidebar

Related Questions

I just need the client-side JavaScript to be able to send a string value
I want to be able to send messages to a remote JBoss server (JBoss
I need to be able to send the content type text/xml to Firefox and
For my application, I need to be able to send an std::vector<std::string> over a
I need to be able to periodically send email alerts to subscribed users. PHP
I need to be able to merge two (very simple) JavaScript objects at runtime.
I need to be able to send text from the clipboard to an application
I need to be able to send out a UDP message and also receive
Where would I need to hook in (at what event) to be able send
I need to be able to send a string message and receive a string

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.