python -> binary usage

. 1 . 1 .10 hex hex converted to other
 2  methods: Function
 3    Decimal Binary rotation: bin (10) -> ' 0b1010 ' Tpye: string type is 0b: represents a binary
 4    decimal turn octal: OCT ( 10) -> ' 0o12 '    Tpye: a string type 0o: represents octal
 5 decimal hex rotation: hex (10) -> ' 0xA '     Tpye: a string type 0x: indicates hexadecimal
 6  method two: the format
 . 7 >>> ' {:} B ' .format (. 9 )
 . 8  ' 1001 ' 
. 9 >>> ' {:} O ' .format(9)
10 '. 11 ' 
. 11 >>> ' {:} X ' .format (10 )
 12 is  ' A ' 
13 is  
14 2 . Other converted to decimal 10:
 15  Method a: int () function
 16 int ( ' 0b1010 ' , 2) - -> 10, or int ( ' 1010 ' , 2) -> 10  
 . 17 int ( ' 0o12 ' ,. 8) -> 10, or int ( ' 12 is ' ,. 8) -> 10  
 18 is int ( ' 0xA ' , 16 ) -> 10, or int ( ' A ',16)   --> 10
19  Note: int (x, y) x is the brackets to be converted must be a string type value, y is the current hexadecimal number
 20  Method two: eval function
 21 is >>> the eval ( ' 1111 ' )
 22 is 1111
 23 is >> > the eval ( ' 0b1111 ' )
 24 15
 25 >>> the eval ( ' 0o1111 ' )
 26 is 585
 27 >>> the eval ( ' 0x1111 ' )
 28 4369
 29  Note: type must be str and the preceding binary flag (0b, 0o , 0x) indispensable
 30  
31 is . 3 . decimal converted to multi-value type of fixed length:
 32  method a:
 33 >>> ' {:} 08B ' .format (. 9 )
 34 is  ' 00001001 ' 
35 >>> ' {:} 06o ' .format (. 9 )
 36  ' 000011 ' 
37 [ >>> ' {:} 06x ' .format (. 9 )
 38 is  ' 000009 ' 
39 Note: ' {:} 08B ' .format (. 9) # : B represents a binary conversion, 08 denotes a high enough to fill 0 8 
40  method two:
 41 is >>> A bin = (. 5) [2 :]
 42 >>>print(a)
43 101
44 >>> c = str.zfill(a,8)
45 >>> print(c)
46 00000101
47 >>> a =oct(9)[2:]
48 >>> print(a)
49 11
50 >>> c = str.zfill(a,8)
51 >>> print(c)
52 00000011
53 >>> 
54 >>> a =hex(20)[2:]#去掉前面0b
55 >>> print(a)
56 14
57 >>> a =hex(20)
58 >>> a
59 '0x14'
60 >>> str.zfill(a,8)
61 '00000x14'
62 >>> 

 

Guess you like

Origin www.cnblogs.com/renke123/p/11029906.html
Recommended