Good memory is better than bad writing, there are some commonly used numbers, string conversion of learning notes
1. Convert a binary number to a string in the specified format
Let's take a look at a small piece of code
>>> a = 0b0100>>> print a4>>> print format (A, ' #0b ') 0b100>>> print format (A, ' 0b ') 100 >>> print Format (A, ' 04b ') 0100>>> print format (A, ' #06b ') # Note that the 0b in the string also takes up two widths, so the width here is 6 to fill 00b0100
The binary of variable A is 0100, and a for direct print is the decimal 4. If you want to convert a to a string in binary form, you need to use the Format function.
Official Documentation: Https://docs.python.org/2/library/string.html#format-specification-mini-language
' # ' applies only to numbers, indicating that the converted format contains prefixes (such as 0b, 0o, 0x)
The ' # ' option is only valid for integers, and only for binary, octal, orhexadecimal output. If present, it specifies that the output would be Prefixedby ' 0b ', ' 0o ', or ' 0x ', respectively.
0, if the width is less than the specified width when the number is converted to a character, the previous 0
>>> a = 0b0100>>> print "a = {0:04b}". Format (a) a = 0100>>> print "A = {0: #06b}". Format (a) a = 0b 0100
2. Convert a string (a different binary) or a number (a different binary, such as 2, 16) to a decimal number
The INT function can meet this requirement and take a look at a small piece of code
>>> Int (0b001) # ref 14>>> int (0xff) # ref 2255>>> int (' 0b1010 ', 2) # ref 310>>> I NT (' 111 ', 2) # ref 47>>> int (' 111 ') # REF 5111
The first parameter in the INT function can be passed into a numeric type, or it can be passed into a character type.
When a digital type is passed in, it is automatically converted to 10 based on the type. "Reference ref."
When passing in a character, you can then specify the second argument, which indicates what the first string is, and then int then converts it to a 10-byte binary. When you do not specify the second parameter (the binary type), the first string is treated as a 10 binary by default. "Reference ref 3, 4, 5"
>>> Help (int) # More information refer to the following document
class Int (object)
| Int (x=0), int or long
| int (x, base=10), int or long
|
|Convert a number or string to an integer, or return 0 if no arguments
| is given. If x is floating point, the conversion truncates towards zero.
| If x is outside the integer range, the function returns a long instead.
|
|if x is not a number or if base is given, then x must a string or
|Unicode object representing an integer literal in the given base.The
| Literal can is preceded by ' + ' or '-' and is surrounded by whitespace.
| The base defaults to 10. Valid bases is 0 and 2-36. Base 0 means to
| Interpret the base from the string as an integer literal.
| >>> int (' 0b100 ', base=0)
| 4
3. Convert a different number into a binary form of a string
The bin function can meet this requirement and take a look at a small piece of code
The bin function only accepts parameters of numeric type
>>> bin (0b0100) ' 0b0100 ' >>> bin (0xff) ' 0b11111111 '
4. Convert a different number into a 16-binary string
Similarly, the hex function can help us to do this and see a small piece of code
The hex function also accepts only numeric parameters
>>> hex ' 0xf ' >>> hex (0b111) ' 0x7 '
5. There is also a small partner, the OCT function, for what, similar to the above bin and hex
>>> Oct (8) ' 010 ' >>> hex (7) ' 07 '
That ' s it!650) this.width=650; "src=" Http://img.baidu.com/hi/ldw/w_0050.gif "alt=" W_0050.gif "/>
Python numbers, character format conversion notes