Last active
December 26, 2015 15:09
-
-
Save cloverrose/7170831 to your computer and use it in GitHub Desktop.
10進数を任意のN進数に変換する。戻り値は数値のリストになっているのでhexの用に適宜文字列に戻す。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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