Binary binary, octal, hex and its principle of Python

Source: Internet
Author: User

#!usr/bin/env python
# Coding:utf-8

def binary ():
"Binary Method and algorithm"
Number = Ten
Number1 =
Number2 =
Print Bin (number), type (Bin (Number1)), Bin (Number2)
#用十进制算法来表示二进制
# except congruential: 10%2=0 put the string bit ' 0 ', 5%2=1 into the string 10 bits ' 10 ', 2%2=0 put in the Hundred ' 010 ', the last remainder into thousands ' 1010 '
L = []
While number>0:
L.insert (0,str (number%2)) #临时列表l每次从左插入计算后的余数
Number /= 2 #每次把Number除法的结果返回判断循环
Print L
s = '. Join (l) #列表转换字符串循环
Print S



def octonary ():
' octal methods and algorithms '
Number = Ten
Number1 =
Number2 =
Print Oct (number), Oct (Number1), Oct (Number2)
# except congruential: 10%8=2 put string bits ' 2 ', remainder into 10 bits '
L = []
While number>0:
L.insert (0,str (number%8))
Number /= 8
Print L
s = '. Join (L)
Print S



def hexadecimal ():
' hexadecimal method and algorithm '
Number =
Number1 =
Number2 =
Print Hex (number), Hex (Number1), Hex (Number2)
# except congruential: 30%16=14 because the single digit more than 10 put will not be recognized, so the letter instead of A=10,b=11,c=12,d==13,e=14,f=15
# Put the string bits ' e ', the remainder into 10 bits ' 1e '
L = []
While number>0:
n=str (number%16) #除余的数赋值变量n后面比对
L.insert (0,n)
Number /=
if n== ': l[0]= ' a ' #比对n是否为两位数10转换字符a
elif n== ' one ': l[0]= ' B ' #以下同理
elif n== ': l[0]= ' C '
elif n== ': l[0]= ' d '
elif n== ': l[0]= ' e '
elif n== ': l[0]= ' F '
Print L
s = '. Join (L)
Print S




If __name__== "__main__":
Binary ()
Octonary ()
Hexadecimal ()

Binary binary, octal, hex and its principle of Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.