Use the Python built-in functions: Bin (), Oct (), int (), Hex () to enable a binary conversion.
Let's look at the description of these built-in functions in the official Python document:
Bin (x)
Convert an integer number to a binary string. The result is a valid Python expression. If X was not a Python int object, it had to define an __index__ () method, which returns an integer.
Oct (x)
Convert an integer number to an octal string. The result is a valid Python expression. If X was not a Python int object, it had to define an __index__ () method, which returns an integer.
int ([number | string[, Base]])
Convert A number or string to an integer. If no arguments is given, return 0. If A number is given, return number.__int__ (). Conversion of floating point numbers to integers truncates towards zero. A string must is a Base-radix integer literal optionally preceded by ' + ' or '-' (with no space in between) and optionally Surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with ' a ' to ' Z ' (or ' a ' to ' Z ') has the values of 35. The default base is 10. The allowed values are 0 and 2-36. Base-2,-8, and-16 literals can be optionally prefixed with 0b/0b, 0o/0o, or 0x/0x, as with the integer literals in code. Base 0 means to interpret exactly as a code literal, so then the actual base is 2, 8, ten, or, and so that int (' 010 ', 0) Is isn't legal, while INT (' 010 ') are, as well as int (' 010 ', 8).
Hex (x)
Convert an integer number to a hexadecimal string. The result is a valid Python expression. If X was not a Python int object, it had to define an __index__ () method, which returns an integer.
↓ |
2 binary |
8 binary |
10 binary |
16 binary |
2 binary |
- |
Bin (int (x, 8)) |
Bin (int (x, 10)) |
Bin (int (x, 16)) |
8 binary |
Oct (int (x, 2)) |
- |
Oct (int (x, 10)) |
Oct (int (x, 16)) |
10 binary |
int (x, 2) |
int (x, 8) |
- |
int (x, 16) |
16 binary |
Hex (int (x, 2)) |
Hex (int (x, 8)) |
Hex (int (x, 10)) |
- |
The return values for Bin (), Oct (), and Hex () are all strings, with 0b, 0o, and 0x prefixes respectively.
Python binary conversions (binary, decimal, and hexadecimal) instances
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# 2/10/16 base Trans wrote by Srcdog on 20th, April, 2009
# ld elements in base 2, 10, 16.
Import Os,sys
# Global Definition
# base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]
base = [STR (x) for x in range] + [Chr (x) for X in range (Ord (' A '), Ord (' a ') +6)]
# Bin2dec
# Binary to decimal: Int (str,n=10)
def bin2dec (String_num):
return str (int (string_num, 2))
# Hex2dec
# Hexadecimal to Decimal
def hex2dec (String_num):
return str (int (string_num.upper (), 16))
# Dec2bin
# decimal to binary: Bin ()
def dec2bin (String_num):
num = Int (string_num)
MID = []
While True:
if num = = 0:break
Num,rem = Divmod (num, 2)
Mid.append (Base[rem])
Return '. Join ([STR (x) for x in Mid[::-1]])
# Dec2hex
# Decimal to octal: Oct ()
# decimal to 16 binary: Hex ()
def dec2hex (String_num):
num = Int (string_num)
MID = []
While True:
if num = = 0:break
Num,rem = Divmod (num, 16)
Mid.append (Base[rem])
Return '. Join ([STR (x) for x in Mid[::-1]])
# Hex2tobin
# hex to binary: Bin (int (str,16))
def hex2bin (String_num):
Return Dec2bin (Hex2dec (String_num.upper ()))
# Bin2Hex
# binary to 16 binary: Hex (int (str,2))
def bin2hex (String_num):
Return Dec2hex (Bin2dec (String_num))
Usage of Python built-in function-based conversions