Skip to content

Instantly share code, notes, and snippets.

@cloverrose
Last active December 26, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloverrose/7170831 to your computer and use it in GitHub Desktop.
Save cloverrose/7170831 to your computer and use it in GitHub Desktop.
10進数を任意のN進数に変換する。戻り値は数値のリストになっているのでhexの用に適宜文字列に戻す。
def n_digits(val, n):
if n == 1:
return [1 for _ in range(val)]
if val == 0:
return [0]
result = []
while val > 0:
val, mod = divmod(val, n)
result.insert(0, mod)
return result
def hex(val):
chars = '0123456789ABCDEF'
return ''.join([chars[x] for x in n_digits(val, 16)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment