2019-07-09 07:03:18 +00:00
|
|
|
""" Convert Base 10 (Decimal) Values to Hexadecimal Representations """
|
|
|
|
|
|
|
|
# set decimal value for each hexadecimal digit
|
|
|
|
values = {
|
2019-10-05 05:14:13 +00:00
|
|
|
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",
|
2019-07-09 07:03:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-04-04 13:25:49 +00:00
|
|
|
def decimal_to_hexadecimal(decimal: float) -> str:
|
2019-07-26 16:28:32 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
take integer decimal value, return hexadecimal representation as str beginning
|
|
|
|
with 0x
|
|
|
|
>>> decimal_to_hexadecimal(5)
|
|
|
|
'0x5'
|
|
|
|
>>> decimal_to_hexadecimal(15)
|
|
|
|
'0xf'
|
|
|
|
>>> decimal_to_hexadecimal(37)
|
|
|
|
'0x25'
|
|
|
|
>>> decimal_to_hexadecimal(255)
|
|
|
|
'0xff'
|
|
|
|
>>> decimal_to_hexadecimal(4096)
|
|
|
|
'0x1000'
|
|
|
|
>>> decimal_to_hexadecimal(999098)
|
|
|
|
'0xf3eba'
|
|
|
|
>>> # negatives work too
|
|
|
|
>>> decimal_to_hexadecimal(-256)
|
|
|
|
'-0x100'
|
|
|
|
>>> # floats are acceptable if equivalent to an int
|
|
|
|
>>> decimal_to_hexadecimal(17.0)
|
|
|
|
'0x11'
|
|
|
|
>>> # other floats will error
|
|
|
|
>>> decimal_to_hexadecimal(16.16) # doctest: +ELLIPSIS
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-09-10 08:31:26 +00:00
|
|
|
AssertionError
|
|
|
|
>>> # strings will error as well
|
|
|
|
>>> decimal_to_hexadecimal('0xfffff') # doctest: +ELLIPSIS
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-09-10 08:31:26 +00:00
|
|
|
AssertionError
|
|
|
|
>>> # results are the same when compared to Python's default hex function
|
|
|
|
>>> decimal_to_hexadecimal(-256) == hex(-256)
|
|
|
|
True
|
2019-07-26 16:28:32 +00:00
|
|
|
"""
|
2023-10-11 18:30:02 +00:00
|
|
|
assert isinstance(decimal, (int, float))
|
|
|
|
assert decimal == int(decimal)
|
2021-04-04 13:25:49 +00:00
|
|
|
decimal = int(decimal)
|
2019-10-05 05:14:13 +00:00
|
|
|
hexadecimal = ""
|
2019-07-26 16:28:32 +00:00
|
|
|
negative = False
|
|
|
|
if decimal < 0:
|
|
|
|
negative = True
|
|
|
|
decimal *= -1
|
2019-07-09 07:03:18 +00:00
|
|
|
while decimal > 0:
|
2019-07-26 16:28:32 +00:00
|
|
|
decimal, remainder = divmod(decimal, 16)
|
2019-07-09 07:03:18 +00:00
|
|
|
hexadecimal = values[remainder] + hexadecimal
|
2019-10-05 05:14:13 +00:00
|
|
|
hexadecimal = "0x" + hexadecimal
|
2019-07-26 16:28:32 +00:00
|
|
|
if negative:
|
2019-10-05 05:14:13 +00:00
|
|
|
hexadecimal = "-" + hexadecimal
|
2019-07-09 07:03:18 +00:00
|
|
|
return hexadecimal
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-07-26 16:28:32 +00:00
|
|
|
import doctest
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-07-26 16:28:32 +00:00
|
|
|
doctest.testmod()
|