Emacs, scripting and anything text oriented.

Binary <> Decimal <> Hex conversion in Python

Kaushal Modi

Python snippets to do number representation conversion among binary/decimal/hexadecimal.

  • Binary -> Decimal

    int('10010100', 2)
  • Hexadecimal -> Decimal

    int('94', 16)
  • Decimal -> Hexadecimal

    hex(148)[2:]

    The [2:] truncates the 0x prefix added to hex output string.

  • Decimal -> Binary

    bin(148)[2:]

    The [2:] truncates the 0b prefix added to binary output string.


Other conversions can be derived from the ones above.

  • Hexadecimal -> Binary

    bin(int('94', 16))[2:]
  • Binary -> Hexadecimal

    hex(int('10010100', 2))[2:]

If you have written a response to this, enter your response post's URL below.

Or, you can send a "comment" webmention (it's OK if you don't know what that means). When asked about your website on an IndieAuth login screen, simply type https://commentpara.de.

Markdown Support**bold**, _italics_, ~~strikethrough~~, [descr](link), `monospace`, ```LANG\nline1\nline2\n``` (Yep, multi-line code blocks too, with syntax highlighting!), auto-hyperlinking.

Webmentions #