I am looking to convert a dec number to a 6 bit binary number.. bin() works fine but omits leading zeros which are important.
for example:
- 0 = 000000
- 1 = 000001
- 2 = 000010
etc… with the largest dec number allowed being 63.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Either what Matt said in the comment (
bin(63)[2:].zfill(6)), or use format strings in Python 2.6+:You can omit the first zero in Python 2.7+ as you can implicitly number groups.