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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:54:58+00:00 2026-05-13T14:54:58+00:00

I need to convert from an integer to a list of size 8 that

  • 0

I need to convert from an integer to a list of size 8 that is the binary representation of that number (number <= 255) and back. Currently I am using these lines

list(bin(my_num)[2:].rjust(8,'0'))
int("".join(my_list),2)

I did some googling, but had a hard time finding relevant information. I’m just curious if there is a faster, or more standard way to do this.

edit:
Would using bit masking make it faster. E.g. something like this

[(my_num>>y)&1 for y in xrange(7,-1,-1)]

Like I mentioned in a comment I am using this for a steganography app I am writing, so I am doing this thousands of times (3 times per pixel in an image), so speed is good.

  • 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-13T14:54:58+00:00Added an answer on May 13, 2026 at 2:54 pm

    In Python 2.6 or newer, use format syntax:

    '{0:0=#10b}'.format(my_num)[2:]
    # '00001010'
    

    One of the neat things about Python strings is that they are sequences. If all you need to do is iterate through the characters, then there is no need to convert the string to a list.

    Edit: For steganography, you might be interested in converting a stream of characters into a stream of bits. Here is how you could do that with generators:

    def str2bits(astr):
        for char in astr:    
            n=ord(char)
            for bit in '{0:0=#10b}'.format(n)[2:]:
                yield int(bit)
    

    And to convert a stream of bits back into a stream of characters:

    def grouper(n, iterable, fillvalue=None):
        # Source: http://docs.python.org/library/itertools.html#recipes
        "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
        return itertools.izip_longest(*[iter(iterable)]*n,fillvalue=fillvalue)
    
    def bits2str(bits):
        for b in grouper(8,bits):
            yield chr(int(''.join(map(str,b)),2))
    

    For example, you could use the above functions like this:

    for b in str2bits('Hi Zvarberg'):
        print b,
    # 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 1 1 0 1 1 0 0 1 1 0 0 0 0 1 0 1 1 1 0 0 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 1 1 1 0 0 1 0 0 1 1 0 0 1 1 1
    
    # To show bits2str is the inverse of str2bits:
    print ''.join([c for c in bits2str(str2bits('Hi Zvarberg'))])
    # Hi Zvarberg
    

    Also, SO guru Ned Batchelder does some steganography-related experiments using Python and PIL here. You may be able to find some useful code there.

    If you find you need more speed (and still want to code this in Python), you may want to look into using numpy.

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

Sidebar

Related Questions

I need to convert an integer to it's ASCII representation from within the Linux
I need to convert a number from 36 -th base into an integer. The
I need to convert from sqlite2 db to sqlite3, is there any tutorial that
I need to convert from a SQLVARCHAR to a string data type Variable definitions
I have problem i need to convert from my Array structure to std::vector<int> ...
I need to convert data from a spreadsheet into insert statements in SQL. I've
I have a need to convert images from CMYK to RGB - not necessarily
i need to convert this HQL: FROM Appointment a WHERE (a.group==null OR :user MEMBER
I need to write a 'simple' util to convert from ASCII to EBCDIC? The
I need to be able to convert from a Delphi Real48 to C# double.

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.