Last active
March 17, 2016 14:36
-
-
Save e3krisztian/9de04045b80d4ae259cf to your computer and use it in GitHub Desktop.
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 tohex1(n): | |
assert 0 <= n <= 15 | |
return '0123456789ABCDEF'[n] | |
def tohex2(n): | |
assert 0 <= n <= 255 | |
return tohex1(n // 16) + tohex1(n % 16) | |
def tohex8(bytes): | |
assert len(bytes) <= 8 | |
result = '' | |
for n in bytes: | |
result += ' ' + tohex2(n) | |
result += ' ' * (8 - len(bytes)) | |
return result | |
def tohex16(bytes): | |
assert len(bytes) <= 16 | |
return tohex8(bytes[:8]) + ' ' + tohex8(bytes[8:]) | |
def hexaddress(number): | |
digits = [] | |
for i in range(8): | |
digits.append(tohex1(number // 16 ** i % 16)) | |
return ''.join(reversed(digits)) | |
assert hexaddress(128) == '00000080' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment