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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:59:51+00:00 2026-05-19T11:59:51+00:00

I want to convert an 32-byte (although I might need other lengths) integer to

  • 0

I want to convert an 32-byte (although I might need other lengths) integer to a bytes object in python. Is there a clean and simple way to do this?

  • 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-19T11:59:52+00:00Added an answer on May 19, 2026 at 11:59 am

    I’m guessing you need a 32-bit integer, and big-endian to boot:

    >>> from ctypes import c_uint32
    >>> l = c_uint32(0x12345678)
    >>> bytes(l)
    b'xV4\x12'
    

    There is c_uint8, c_uint16 and c_uint64 as well. For longer ints you need to make it manually, using divmod(x, 256).

    >>> def bytify(v):
    ...   v, r = divmod(v, 256)
    ...   yield r
    ...   if v == 0:
    ...      raise StopIteration
    ...   for r in bytify(v):
    ...     yield r
    ... 
    >>> [x for x in bytify(0x12345678)]
    [120, 86, 52, 18]
    >>> bytes(bytify(0x12345678))
    b'xV4\x12
    >>> bytes(bytify(0x123456789098765432101234567890987654321))
    b'!Ce\x87\t\x89gE#\x01!Ce\x87\t\x89gE#\x01'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert an integer ( int or long ) a big-endian byte
I want to convert object value to byte array in c#. EX: step 1.
I want to convert two ASCII bytes to one hexadecimal byte. eg. 0x30 0x43
I want to convert a number that is in PRTime format (a 64-bit integer
I want to convert a byte* to a byte[] , but I also want
I want to convert 24 byte BCD to ASCII.Im trying to write some code
Is there any fast way to convert given byte (like, by number - 65
How do I convert byte[] into a PDF document in C#? I want to
I have a hexadecimal value, 07A5953EE7592CE8871EE287F9C0A5FBC2BB43695589D95E76A4A9D37019C8 Which I want to convert to a byte
I want to convert char lineOneC[8] = {0,1,1,0,0,0,0,1}; into byte lineOneB = B01100001; How

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.