Skip to content

Instantly share code, notes, and snippets.

@hungtatai
Last active November 8, 2016 03:54
Show Gist options
  • Save hungtatai/b39163b93bca2255ffdc826aa73838dc to your computer and use it in GitHub Desktop.
Save hungtatai/b39163b93bca2255ffdc826aa73838dc to your computer and use it in GitHub Desktop.

vim /usr/bin/b2b

#! /usr/bin/env python

import sys

from_num, from_base = (sys.argv[1].split('/') + ["10"])[:2]

if len(sys.argv) > 2:
    to_base = sys.argv[2]
else:
    to_base = 10


def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
    return ((num == 0) and numerals[0]) or (baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b])

def FromBaseNtoBaseN(from_num, from_base, to_base):
    return baseN( int(str(from_num), int(from_base)), int(to_base) ) 

print FromBaseNtoBaseN(from_num, from_base, to_base)

chmod +x /usr/bin/b2b

Usage:

$ b2b deadbeef/16 10
3735928559

$ b2b deadbeef/16 2
11011110101011011011111011101111

$ b2b deadbeef/16
3735928559

$ b2b 100/10 16      
64

$ b2b 100 16      
64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment