Skip to content

added decimal to hexadecimal conversion #977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions conversions/decimal_to_hexadecimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
""" Convert Base 10 (Decimal) Values to Hexadecimal Representations """

# set decimal value for each hexadecimal digit
values = {
0:'0',
1:'1',
2:'2',
3:'3',
4:'4',
5:'5',
6:'6',
7:'7',
8:'8',
9:'9',
10:'a',
11:'b',
12:'c',
13:'d',
14:'e',
15:'f'
}

def decimal_to_hexadecimal(decimal):
""" take decimal value, return hexadecimal representation as str """
hexadecimal = ''
while decimal > 0:
remainder = decimal % 16
decimal -= remainder
hexadecimal = values[remainder] + hexadecimal
decimal /= 16
return hexadecimal

def main():
""" print test cases """
print("5 in hexadecimal is", decimal_to_hexadecimal(5))
print("15 in hexadecimal is", decimal_to_hexadecimal(15))
print("37 in hexadecimal is", decimal_to_hexadecimal(37))
print("255 in hexadecimal is", decimal_to_hexadecimal(255))
print("4096 in hexadecimal is", decimal_to_hexadecimal(4096))
print("999098 in hexadecimal is", decimal_to_hexadecimal(999098))

if __name__ == '__main__':
main()