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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:58:39+00:00 2026-05-18T00:58:39+00:00

There’s probably someone else who asked a similar question, but I didn’t take much

  • 0

There’s probably someone else who asked a similar question, but I didn’t take much time to search for this, so just point me to it if someone’s already answered this.

I’m trying to take an integer (or long) and turn it into a string, in a very specific way.

The goal is essentially to split the integer into 8-bit segments, then take each of those segments and get the corresponding ASCII character for that chunk, then glue the chunks together.

This is easy to implement, but I’m not sure I’m going about it in the most efficient way.

>>> def stringify(integer):
        output = ""
        part = integer & 255
        while integer != 0:
                output += chr(part)
                integer = integer >> 8
        return output
>>> stringify(10)
'\n'
>>> stringify(10 << 8 | 10)
'\n\n'
>>> stringify(32)
' '

Is there a more efficient way to do this?
Is this built into Python?

EDIT:

Also, as this will be run sequentially in a tight loop, is there some way to streamline it for such use?

>>> for n in xrange(1000):  ## EXAMPLE!
        print stringify(n)
...
  • 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-18T00:58:40+00:00Added an answer on May 18, 2026 at 12:58 am

    struct can easily do this for integers up to 64 bits in size. Any larger will require you to carve the number up first.

    >>> struct.pack('>Q', 12345678901234567890)
    '\xabT\xa9\x8c\xeb\x1f\n\xd2'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are nice SO question and answers about this issue, but these options didn't
There was a similar question posted asking in general about alternatives to Qt. Well,
This could be a duplicate question, but I have no idea what search terms
There are plenty of questions and answers to this like this question but I
There is about 2000 lines of this, so manually would probably take more work
There is lots of answers for that question on SO but none for javascript...
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There are many Subset-sum questions and answers around at SO, but somehow I can't
There was a thread on this in comp.lang.javascript recently where victory was announced but
There is a process that seems to be running all the time in SQL

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.