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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:29:08+00:00 2026-06-10T02:29:08+00:00

I have to get bytes array and send it into socket. The structure looks

  • 0

I have to get bytes array and send it into socket.

The structure looks like: 1 byte + 2 bytes + 2 bytes.

First byte is number ‘5’, second 2 bytes should be taken from variable first, third 2 bytes should be taken from variable second. What’s the right way to do this in python?

id = 5      # Fill as 1 byte
first = 42  # Fill as 2 bytes
second = 58 # The same as first
  • 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-10T02:29:09+00:00Added an answer on June 10, 2026 at 2:29 am

    Use the struct module:

    >>> import struct
    >>> id, first, second = 5, 42, 58
    >>> struct.pack('>bhb', id, first, second)
    b'\x05\x00*:'
    

    You may want to figure out if your data is a) little or big endian and b) is signed or unsigned; the example above use big-endian ordering and signed values.

    The result (in python 3) is a bytes object.

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

Sidebar

Related Questions

I'm trying to get from an Android Uri to a byte array. I have
I have a List<byte[]> . Each byte array is of size 16bytes. The first
I have an object with a char Array; where the first 5 bytes(char in
How to get the string length in bytes in nodejs? If I have a
I am trying to get file content in bytes in Android application. I have
I have problem with sending objects via TCPClient. At first, I serialize them into
I have a java server listening on a Socket. I can send and receive
I have a .NET web service that takes a byte array. I have a
In my application perticular message structure/format are send and received. When I have to
I have binary data in a file that I can read into a byte

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.