Implementation of format binary conversion and deletion of binary prefix in Python

Time:2021-10-21

10 decimal, hexadecimal, octal and binary conversion:

(#: reserved hexadecimal prefix)

Implementation of format binary conversion and deletion of binary prefix in Python

For those with hexadecimal prefix, such as “0x” “0o” “0b”, you can directly add ‘x’ and ‘o’ ‘B’ to delete them:

Implementation of format binary conversion and deletion of binary prefix in Python

Other tips:

format(num, ",") Fast division of millennia

Implementation of format binary conversion and deletion of binary prefix in Python

How many digits are the numbers to complete, ^ center > right alignment < left alignment

*^10 ⟹ there are 10 digits in total, in the middle, and the rest are filled with *

Implementation of format binary conversion and deletion of binary prefix in Python

Supplement: Python binary conversion function and prefix interception

“Life is short, I use Python”. Powerful Python must also have its own built-in binary conversion function.

Decimal to other decimal:

Decimal number s:
A = bin (s) / / decimal to binary
B = OCT (s) / / decimal to octal
C = hex (s) / / decimal to hexadecimal

Convert other decimal to decimal:

s: Converted String

r: Number of hexadecimal digits to convert 2,6,18

int(s,r)

10 after hexadecimal, octal and binary conversion, prefixes will appear, corresponding to “0x”, “0o” and “0b” respectively, as shown in the figure:

Implementation of format binary conversion and deletion of binary prefix in Python

If you want to get the following specific values, you can finally intercept the string, str

str = ‘0123456789'

The above is my personal experience. I hope I can give you a reference, and I hope you can support developepper. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.

Recommended Today

Git usage specification

​ 1. Basic operation of GIT -Git init if a project needs to be managed using git, it needs to be initialized -Git status: check the status of the current code (red: in the Development Zone, green: in the temporary storage zone, nothing to commit: there is no change in the Development Zone) -Git checkout […]