All pastes #816947 Raw Edit

base converter

public python v1 · immutable
·published 2007-12-15 02:53 UTC
rendered paste body
>>> def calc(chars, n, pad=0):	"""Convert 'n' to base defined by string 'chars', with padding 'pad'"""	output = ''	while n != 0:		output = chars[n % len(chars)] + output		n -= n % len(chars)		n /= len(chars)		if len(output) < pad:			return ('0' * (pad - len(output))) + output		else:			return output		>>> calc('0123456789abcdef', 42, 4)'000a'>>> calc('0123456789abcdef', 42, pad=4)'000a'